-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathquick3_string.go
55 lines (49 loc) · 1021 Bytes
/
quick3_string.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
package algs4
// Quick3String ...
type Quick3String struct {
CUTOFF int // cutoff to insertion sort
R int // extended ASCII alphabet size
aux []string
}
// NewQuick3String ...
func NewQuick3String(a []string) *Quick3String {
m := &Quick3String{CUTOFF: 15, R: 256, aux: make([]string, len(a))}
m.sort(a, 0, len(a)-1, 0)
return m
}
func (m *Quick3String) insertion(a []string, lo, hi, d int) {
for i := lo; i <= hi; i++ {
for j := i; j > lo && a[j][d] < a[j-1][d]; j-- {
a[j], a[j-1] = a[j-1], a[j]
}
}
}
func (m *Quick3String) sort(a []string, lo, hi, d int) {
if hi <= lo+m.CUTOFF {
m.insertion(a, lo, hi, d)
return
}
lt, gt := lo, hi
v := a[lo][d]
for i := lo + 1; i <= gt; {
t := a[i][d]
if t < v {
m.exch(a, lt, i)
lt++
i++
} else if t > v {
m.exch(a, i, gt)
gt--
} else {
i++
}
}
m.sort(a, lo, lt-1, d)
if v >= 0 {
m.sort(a, lt, gt, d+1)
}
m.sort(a, gt+1, hi, d)
}
func (m *Quick3String) exch(a []string, i, j int) {
a[i], a[j] = a[j], a[i]
}