-
Notifications
You must be signed in to change notification settings - Fork 6
/
sortyCol.go
205 lines (178 loc) · 3.92 KB
/
sortyCol.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* Copyright (c) 2019, Serhat Şevki Dinçer.
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package sorty
import "sync/atomic"
// Lesser represents a collection of comparable elements.
type Lesser interface {
// Len is the number of elements in the collection. First element has index 0.
Len() int
// Less (a strict ordering like < or >) reports if element-i should come
// before element-k:
// Less(i,k) && Less(k,r) => Less(i,r)
// Less(i,k) => ! Less(k,i)
Less(i, k int) bool
}
// Collection is same with standard library's sort.Interface. It represents a
// collection of sortable (as per Less) elements.
type Collection interface {
Lesser
// Swaps i-th and k-th elements.
Swap(i, k int)
}
// IsSorted checks if ar is sorted.
func IsSorted(ar Lesser) bool {
for i := ar.Len() - 1; i > 0; i-- {
if ar.Less(i, i-1) {
return false
}
}
return true
}
// insertion sort
func insertion(ar Collection, lo, hi int) {
for l, h := mid(lo, hi+1)-2, hi; l >= lo; l, h = l-1, h-1 {
if ar.Less(h, l) {
ar.Swap(h, l)
}
}
for h := lo + 1; h <= hi; h++ {
for l := h; ar.Less(l, l-1); {
ar.Swap(l, l-1)
l--
if l <= lo {
break
}
}
}
}
// set such that ar[l,l+1] <= ar[m] = pivot <= ar[h-1,h]
func pivot(ar Collection, l, h int) (a, b, c int) {
m := mid(l, h)
if ar.Less(h, l) {
ar.Swap(h, l)
}
if ar.Less(h, m) {
ar.Swap(h, m)
} else if ar.Less(m, l) {
ar.Swap(m, l)
}
// ar[l] <= ar[m] <= ar[h]
k, h := h, h-1
if ar.Less(h, m) {
k--
ar.Swap(h, m)
if ar.Less(m, l) {
ar.Swap(m, l)
}
}
l++
if ar.Less(m, l) {
ar.Swap(m, l)
if k > h && ar.Less(h, h+1) {
k = h
}
if ar.Less(k, m) {
ar.Swap(k, m)
}
}
return l + 1, h - 1, m
}
// partition ar into two groups: >= and <= pivot
func partition(ar Collection, l, h int) (int, int) {
l, h, pv := pivot(ar, l, h)
out:
for ; l < h; l, h = l+1, h-1 {
if ar.Less(h, pv) { // avoid unnecessary comparisons
for {
if ar.Less(pv, l) {
ar.Swap(l, h)
break
}
l++
if l >= h {
break out
}
}
} else if ar.Less(pv, l) { // extend ranges in balance
for {
h--
if l >= h {
break out
}
if ar.Less(h, pv) {
ar.Swap(l, h)
break
}
}
}
}
if l == h {
if ar.Less(pv, l) { // classify mid element
h--
} else {
l++
}
}
return l, h
}
// Sort concurrently sorts ar.
func Sort(ar Collection) {
var (
arhi, mli = ar.Len() - 1, Mli >> 2
ng uint32 // number of sorting goroutines including this
done chan bool // end signal
srt, gsrt func(int, int) // recursive & new-goroutine sort functions
)
gsrt = func(lo, hi int) {
srt(lo, hi)
if atomic.AddUint32(&ng, ^uint32(0)) == 0 { // decrease goroutine counter
done <- false // we are the last, all done
}
}
srt = func(lo, hi int) { // assumes hi-lo >= mli
start:
l, h := partition(ar, lo, hi)
if h-lo < hi-l {
h, hi = hi, h // [lo,h] is the longer range
l, lo = lo, l
}
// branches below are optimally laid out for fewer jumps
// at least one short range?
if hi-l < mli {
insertion(ar, l, hi)
if h-lo < mli { // two short ranges?
insertion(ar, lo, h)
return
}
hi = h
goto start
}
// range not long enough for new goroutine? max goroutines?
// not atomic but good enough
if hi-l < Mlr || ng >= Mxg {
srt(l, hi) // start a recursive sort on the shorter range
hi = h
goto start
}
if atomic.AddUint32(&ng, 1) == 0 { // increase goroutine counter
panic("Sort: counter overflow")
}
go gsrt(lo, h) // start a new-goroutine sort on the longer range
lo = l
goto start
}
if arhi > 2*Mlr {
ng, done = 1, make(chan bool, 1)
gsrt(0, arhi) // start master sort
<-done
return
}
if arhi >= mli {
srt(0, arhi) // single goroutine
return
}
insertion(ar, 0, arhi)
}