-
Notifications
You must be signed in to change notification settings - Fork 108
/
RadixSort.go
59 lines (44 loc) · 969 Bytes
/
RadixSort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import (
"fmt"
"strconv"
)
func largest(arr []int) int {
max := 0
for i := 0; i < len(arr); i++ {
if arr[i] > max {
max = arr[i]
}
}
return max
}
func radixSort(arr []int) []int {
// base 10
max := largest(arr)
size := len(arr)
significantDigit := 1
partial := make([]int, size, size)
for max / significantDigit > 0 {
bucket := [10]int{0}
for i := 0; i < size; i++ {
bucket[(arr[i] / significantDigit) % 10]++
}
for i := 1; i < 10; i++ {
bucket[i] += bucket[i - 1]
}
for i := size - 1; i >= 0; i-- {
bucket[(arr[i] / significantDigit) % 10]--
partial[bucket[(arr[i] / significantDigit) % 10]] = arr[i]
}
for i := 0; i < size; i++ {
arr[i] = partial[i]
}
significantDigit *= 10
}
return arr
}
func main() {
unsorted :=[]int{9386, 234, 369, 4863, 611, 1, 0, 55, 69, 928, 2267, 28}
fmt.Println("Unsorted:", unsorted, "\n")
sorted := radixSort(unsorted)
fmt.Println("\nSorted:", sorted, "\n")
}