Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d8f0817

Browse files
add radix sort
1 parent 048de6b commit d8f0817

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* Selection sort (Сортировка выбором): [example](https://github.com/dreddsa5dies/algorithm/tree/master/selectionSort)
1313
* Heap sort (Пирамидальная сортировка, «Сортировка кучей»): [example](https://github.com/dreddsa5dies/algorithm/tree/master/heapSort)
1414
* Merge sort (Сортировка слиянием): [example](https://github.com/dreddsa5dies/algorithm/tree/master/mergeSort)
15+
* Radix sort (Поразрядная сортировка): [example](https://github.com/dreddsa5dies/algorithm/tree/master/radixSort)
1516
### Search:
1617
* Binary search (Бинарный поиск): [example](https://github.com/dreddsa5dies/algorithm/tree/master/binarySearch)
1718
* Breadth-first search, BFS (Поиск в ширину (англ. breadth-first search, BFS) — метод обхода графа и поиска пути в графе): [example](https://github.com/dreddsa5dies/algorithm/tree/master/BFS)

‎radixSort/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Поразрядная сортировка (англ. radix sort) — алгоритм сортировки, который выполняется за линейное время.

‎radixSort/radixSort.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Thanks https://austingwalters.com/radix-sort-in-go/
2+
package main
3+
4+
import (
5+
"fmt"
6+
"strconv"
7+
8+
"github.com/dreddsa5dies/algorithm/util"
9+
)
10+
11+
func main() {
12+
s1 := util.RandomInt() // срез int
13+
14+
fmt.Println("Unsorted List:", s1)
15+
16+
sortedList := radixSort(s1)
17+
18+
fmt.Println("Sorted List:", sortedList)
19+
}
20+
21+
// Finds the largest number in an array
22+
func findLargestNum(array []int) int {
23+
largestNum := 0
24+
25+
for i := 0; i < len(array); i++ {
26+
if array[i] > largestNum {
27+
largestNum = array[i]
28+
}
29+
}
30+
return largestNum
31+
}
32+
33+
// Radix Sort
34+
func radixSort(array []int) []int {
35+
36+
fmt.Println("Running Radix Sort on Unsorted List")
37+
38+
// Base 10 is used
39+
largestNum := findLargestNum(array)
40+
size := len(array)
41+
significantDigit := 1
42+
semiSorted := make([]int, size, size)
43+
44+
// Loop until we reach the largest significant digit
45+
for largestNum/significantDigit > 0 {
46+
47+
fmt.Println("\tSorting: "+strconv.Itoa(significantDigit)+"'s place", array)
48+
49+
bucket := [10]int{0}
50+
51+
// Counts the number of "keys" or digits that will go into each bucket
52+
for i := 0; i < size; i++ {
53+
bucket[(array[i]/significantDigit)%10]++
54+
}
55+
56+
// Add the count of the previous buckets
57+
// Acquires the indexes after the end of each bucket location in the array
58+
// Works similar to the count sort algorithm
59+
for i := 1; i < 10; i++ {
60+
bucket[i] += bucket[i-1]
61+
}
62+
63+
// Use the bucket to fill a "semiSorted" array
64+
for i := size - 1; i >= 0; i-- {
65+
bucket[(array[i]/significantDigit)%10]--
66+
semiSorted[bucket[(array[i]/significantDigit)%10]] = array[i]
67+
}
68+
69+
// Replace the current array with the semisorted array
70+
for i := 0; i < size; i++ {
71+
array[i] = semiSorted[i]
72+
}
73+
74+
fmt.Println("\n\tBucket: ", bucket)
75+
76+
// Move to next significant digit
77+
significantDigit *= 10
78+
}
79+
80+
return array
81+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /