-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdqsort.go
297 lines (254 loc) · 7.2 KB
/
pdqsort.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package gosort
import (
"math"
"sort"
)
// PdqSort1 implements CLRS bad pivot quicksort with pdq bad pivot pattern breaking
//
// Bad-pattern busting is copied from the Rust pdqsort implementation, https://docs.rs/pdqsort/0.1.0/src/pdqsort/lib.rs.html#427
func PdqSort1(nums []int) {
pdqSort1(nums, 0, len(nums)-1, allowedBadPartitions(len(nums)-1, 0))
}
// PdqSortIface1 implements CLRS bad pivot quicksort with pdq bad pivot pattern breaking on a sort.Interface
//
// This is to verify the effect of pattern-busting on the killer adversary
func PdqSortIface1(data sort.Interface) {
pdqSortIface1(data, 0, data.Len()-1, allowedBadPartitions(data.Len()-1, 0))
}
// PdqSort2 modifies pkg/sort's introsort quicksort with pdq bad pivot pattern breaking
func PdqSort2(nums []int) {
n := len(nums)
pdqSort2(nums, 0, n, maxDepth(n), allowedBadPartitions(0, n))
}
// PdqSort3 implements CLRS randomized pivot quicksort with pdq bad pivot pattern breaking
func PdqSort3(nums []int) {
pdqSort3(nums, 0, len(nums)-1, allowedBadPartitions(len(nums)-1, 0))
}
func allowedBadPartitions(a, b int) int {
return int(math.Log2(float64(b - a)))
}
func breakPatterns(nums []int) {
n := len(nums)
if n >= 4 {
nums[0], nums[n/2] = nums[n/2], nums[0]
nums[n-1], nums[n-n/2] = nums[n-n/2], nums[n-1]
if n >= 8 {
nums[1], nums[n/2+1] = nums[n/2+1], nums[1]
nums[2], nums[n/2+2] = nums[n/2+2], nums[2]
nums[n-2], nums[n-n/2-1] = nums[n-n/2-1], nums[n-2]
nums[n-3], nums[n-n/2-2] = nums[n-n/2-2], nums[n-3]
}
}
}
func breakPatternsIface(data sort.Interface, a, b int) {
n := b - a
if n >= 4 {
data.Swap(a, a+n/2)
data.Swap(b-1, b-n/2)
if n >= 8 {
data.Swap(a+1, a+1+n/2)
data.Swap(a+2, a+2+n/2)
data.Swap(b-2, b-n/2-1)
data.Swap(b-3, b-n/2-2)
}
}
}
func pdqSort2(nums []int, a, b, maxDepth, badAllowed int) {
for b-a > 12 { // Use ShellSort for slices <= 12 elements
if badAllowed == 0 {
heapSort(nums, a, b)
return
}
// inherently always trying to drop back down to heapsort
// so it's a bit different from the C++ pdqsort
maxDepth--
mlo, mhi := doPdqPivot(nums, a, b)
pivotPos := mlo // this is where the pivot landed after the partitioning
lSize := pivotPos - a
rSize := b - (pivotPos + 1)
highlyUnbalanced := lSize < len(nums)/8 || rSize < len(nums)/8
if highlyUnbalanced {
// do some specific swaps here per pdqsort
// https://github.com/orlp/pdqsort/blob/master/pdqsort.h#L467
badAllowed--
// too many bad partitions encountered, time to bail
if badAllowed == 0 {
heapSort(nums, a, b)
return
}
breakPatterns(nums[a:mlo])
breakPatterns(nums[mhi:b])
}
// Avoiding recursion on the larger subproblem guarantees
// a stack depth of at most lg(b-a).
if mlo-a < b-mhi {
pdqSort2(nums, a, mlo, maxDepth, badAllowed)
a = mhi // i.e., quickSort(data, mhi, b)
} else {
pdqSort2(nums, mhi, b, maxDepth, badAllowed)
b = mlo // i.e., quickSort(data, a, mlo)
}
}
if b-a > 1 {
// Do ShellSort pass with gap 6
// It could be written in this simplified form cause b-a <= 12
for i := a + 6; i < b; i++ {
if nums[i] <= nums[i-6] {
nums[i], nums[i-6] = nums[i-6], nums[i]
}
}
insertionSort(nums, a, b)
}
}
func pdqSort1(nums []int, p, r, badAllowed int) {
if p < r {
q, pivot := partition1(nums, p, r)
lSize := pivot - p
rSize := r - (pivot + 1)
highlyUnbalanced := lSize < len(nums)/8 || rSize < len(nums)/8
if highlyUnbalanced {
// do some specific swaps here per pdqsort
// https://github.com/orlp/pdqsort/blob/master/pdqsort.h#L467
badAllowed--
// too many bad partitions encountered, time to bail
if badAllowed == 0 {
heapSort(nums, p, r)
return
}
breakPatterns(nums[p:q])
breakPatterns(nums[q+1 : r+1])
}
pdqSort1(nums, p, q-1, badAllowed)
pdqSort1(nums, q+1, r, badAllowed)
}
}
func pdqSortIface1(data sort.Interface, p, r, badAllowed int) {
if p < r {
q, pivot := partitionIface1(data, p, r)
lSize := pivot - p
rSize := r - (pivot + 1)
highlyUnbalanced := lSize < data.Len()/8 || rSize < data.Len()/8
if highlyUnbalanced {
// do some specific swaps here per pdqsort
// https://github.com/orlp/pdqsort/blob/master/pdqsort.h#L467
badAllowed--
// too many bad partitions encountered, time to bail
if badAllowed == 0 {
heapSortIface(data, p, r)
return
}
breakPatternsIface(data, p, q)
breakPatternsIface(data, q+1, r+1)
}
pdqSortIface1(data, p, q-1, badAllowed)
pdqSortIface1(data, q+1, r, badAllowed)
}
}
func doPdqPivot(nums []int, lo, hi int) (mlo, mhi int) {
m := int(uint(lo+hi) >> 1) // Written like this to avoid integer overflow.
if hi-lo > 40 {
// Tukey's ``Ninther,'' median of three medians of three.
s := (hi - lo) / 8
medianOfThree(nums, lo, lo+s, lo+2*s)
medianOfThree(nums, m, m-s, m+s)
medianOfThree(nums, hi-1, hi-1-s, hi-1-2*s)
}
medianOfThree(nums, lo, m, hi-1)
// Invariants are:
// nums[lo] = pivot (set up by ChoosePivot)
// nums[lo < i < a] < pivot
// nums[a <= i < b] <= pivot
// nums[b <= i < c] unexamined
// nums[c <= i < hi-1] > pivot
// nums[hi-1] >= pivot
pivot := lo
a, c := lo+1, hi-1
for ; a < c && nums[a] <= nums[pivot]; a++ {
}
b := a
for {
for ; b < c && !(nums[pivot] <= nums[b]); b++ {
}
for ; b < c && nums[pivot] <= nums[c-1]; c-- {
}
if b >= c {
break
}
nums[b], nums[c-1] = nums[c-1], nums[b]
b++
c--
}
// If hi-c<3 then there are duplicates (by property of median of nine).
// Let's be a bit more conservative, and set border to 5.
protect := hi-c < 5
if !protect && hi-c < (hi-lo)/4 {
// Lets test some points for equality to pivot
dups := 0
//if !(nums[pivot] <= nums[hi-1]) {
if nums[pivot] == nums[hi-1] {
nums[c], nums[hi-1] = nums[hi-1], nums[c]
c++
dups++
}
//if !(nums[b-1] <= nums[pivot]) {
if nums[b-1] == nums[pivot] {
b--
dups++
}
// m-lo = (hi-lo)/2 > 6
// b-lo > (hi-lo)*3/4-1 > 8
// ==> m < b ==> data[m] <= pivot
//if !(nums[m] <= nums[pivot]) {
if nums[m] == nums[pivot] {
nums[m], nums[b-1] = nums[b-1], nums[m]
b--
dups++
}
// if at least 2 points are equal to pivot, assume skewed distribution
protect = dups > 1
}
if protect {
// Protect against a lot of duplicates
// Add invariant:
// data[a <= i < b] unexamined
// data[b <= i < c] = pivot
for {
for ; a < b && nums[b-1] == nums[pivot]; b-- {
}
for ; a < b && nums[a] < nums[pivot]; a++ {
}
if a >= b {
break
}
// data[a] == pivot; data[b-1] < pivot
nums[a], nums[b-1] = nums[b-1], nums[a]
a++
b--
}
}
// Swap pivot into middle
nums[pivot], nums[b-1] = nums[b-1], nums[pivot]
return b - 1, c
}
func pdqSort3(nums []int, p, r, badAllowed int) {
if p < r {
q, pivot := partition3(nums, p, r)
lSize := pivot - p
rSize := r - (pivot + 1)
highlyUnbalanced := lSize < len(nums)/8 || rSize < len(nums)/8
if highlyUnbalanced {
// do some specific swaps here per pdqsort
// https://github.com/orlp/pdqsort/blob/master/pdqsort.h#L467
badAllowed--
// too many bad partitions encountered, time to bail
if badAllowed == 0 {
heapSort(nums, p, r)
return
}
breakPatterns(nums[p:q])
breakPatterns(nums[q+1 : r+1])
}
pdqSort3(nums, p, q-1, badAllowed)
pdqSort3(nums, q+1, r, badAllowed)
}
}