-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmsd.go
50 lines (41 loc) · 1.01 KB
/
msd.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
package sort
import "github.com/youngzhu/algs4-go/strings"
// Sort an array of strings using MSD radix sort
// Rearranges the array of extended ASCII strings in ascending order
func MSDSort(a []string) {
n := len(a)
aux := make([]string, n)
sort(a, aux, 0, n-1, 0)
}
// sort from a[lo] to a[hi], starting at the dth character
func sort(a, aux []string, lo, hi, d int) {
// cutoff to insertion sort for small subarrays
if hi < lo+cutoff {
insertion(a, lo, hi, d)
return
}
// compute frequency counts
count := make([]int, strings.R+2)
for i := lo; i <= hi; i++ {
c := a[i][d]
count[c+2]++
}
// transform counts to indicies
for r := 0; r < strings.R; r++ {
count[r+1] += count[r]
}
// distribute
for i := lo; i <= hi; i++ {
c := a[i][d]
aux[count[c+1]] = a[i]
count[c+1]++
}
// copy back
for i := lo; i <= hi; i++ {
a[i] = aux[i-lo]
}
// recursively sort for each character (excludes sentinel -1)
for r := 0; r < strings.R; r++ {
sort(a, aux, lo+count[r], lo+count[r+1]-1, d+1)
}
}