-
-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathradixsort.go
More file actions
49 lines (40 loc) · 1.04 KB
/
radixsort.go
File metadata and controls
49 lines (40 loc) · 1.04 KB
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
package RadixSort
// Get maximum value of an array
func getMax(arr []int, n int) int {
max := arr[0]
for i := 1; i < n; i++ {
if arr[i] > max {
max = arr[i]
}
}
return max
}
// A function to do counting sort of arr[] according to the digit represented by exp.
func countSort(arr []int, n int, exp int) {
output := make([]int, n)
var count = []int{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
i := 0
// Store count of occurrences in count[]
for i = 0; i < n; i++ {
count[(arr[i]/exp)%10]++
}
// Change count[i] so that count[i] now contains actual position of this digit in output[]
for i = 1; i < 10; i++ {
count[i] += count[i-1]
}
// Build the output array
for i = n - 1; i >= 0; i-- {
output[count[(arr[i]/exp)%10]-1] = arr[i]
count[(arr[i]/exp)%10]--
}
// Copy the output array to arr[], so that arr[] now contains sorted numbers according to curent digit
for i = 0; i < n; i++ {
arr[i] = output[i]
}
}
func radixsort(arr []int, n int) {
m := getMax(arr, n)
for exp := 1; m/exp > 0; exp *= 10 {
countSort(arr, n, exp)
}
}