Source file
src/slices/zsortordered.go
1
2
3
4
5
6
7 package slices
8
9 import "cmp"
10
11
12 func insertionSortOrdered[E cmp.Ordered](data []E, a, b int) {
13 for i := a + 1; i < b; i++ {
14 for j := i; j > a && cmp.Less(data[j], data[j-1]); j-- {
15 data[j], data[j-1] = data[j-1], data[j]
16 }
17 }
18 }
19
20
21
22 func siftDownOrdered[E cmp.Ordered](data []E, lo, hi, first int) {
23 root := lo
24 for {
25 child := 2*root + 1
26 if child >= hi {
27 break
28 }
29 if child+1 < hi && cmp.Less(data[first+child], data[first+child+1]) {
30 child++
31 }
32 if !cmp.Less(data[first+root], data[first+child]) {
33 return
34 }
35 data[first+root], data[first+child] = data[first+child], data[first+root]
36 root = child
37 }
38 }
39
40 func heapSortOrdered[E cmp.Ordered](data []E, a, b int) {
41 first := a
42 lo := 0
43 hi := b - a
44
45
46 for i := (hi - 1) / 2; i >= 0; i-- {
47 siftDownOrdered(data, i, hi, first)
48 }
49
50
51 for i := hi - 1; i >= 0; i-- {
52 data[first], data[first+i] = data[first+i], data[first]
53 siftDownOrdered(data, lo, i, first)
54 }
55 }
56
57
58
59
60
61
62
63 func pdqsortOrdered[E cmp.Ordered](data []E, a, b, limit int) {
64 const maxInsertion = 12
65
66 var (
67 wasBalanced = true
68 wasPartitioned = true
69 )
70
71 for {
72 length := b - a
73
74 if length <= maxInsertion {
75 insertionSortOrdered(data, a, b)
76 return
77 }
78
79
80 if limit == 0 {
81 heapSortOrdered(data, a, b)
82 return
83 }
84
85
86 if !wasBalanced {
87 breakPatternsOrdered(data, a, b)
88 limit--
89 }
90
91 pivot, hint := choosePivotOrdered(data, a, b)
92 if hint == decreasingHint {
93 reverseRangeOrdered(data, a, b)
94
95
96
97 pivot = (b - 1) - (pivot - a)
98 hint = increasingHint
99 }
100
101
102 if wasBalanced && wasPartitioned && hint == increasingHint {
103 if partialInsertionSortOrdered(data, a, b) {
104 return
105 }
106 }
107
108
109
110 if a > 0 && !cmp.Less(data[a-1], data[pivot]) {
111 mid := partitionEqualOrdered(data, a, b, pivot)
112 a = mid
113 continue
114 }
115
116 mid, alreadyPartitioned := partitionOrdered(data, a, b, pivot)
117 wasPartitioned = alreadyPartitioned
118
119 leftLen, rightLen := mid-a, b-mid
120 balanceThreshold := length / 8
121 if leftLen < rightLen {
122 wasBalanced = leftLen >= balanceThreshold
123 pdqsortOrdered(data, a, mid, limit)
124 a = mid + 1
125 } else {
126 wasBalanced = rightLen >= balanceThreshold
127 pdqsortOrdered(data, mid+1, b, limit)
128 b = mid
129 }
130 }
131 }
132
133
134
135
136
137 func partitionOrdered[E cmp.Ordered](data []E, a, b, pivot int) (newpivot int, alreadyPartitioned bool) {
138 data[a], data[pivot] = data[pivot], data[a]
139 i, j := a+1, b-1
140
141 for i <= j && cmp.Less(data[i], data[a]) {
142 i++
143 }
144 for i <= j && !cmp.Less(data[j], data[a]) {
145 j--
146 }
147 if i > j {
148 data[j], data[a] = data[a], data[j]
149 return j, true
150 }
151 data[i], data[j] = data[j], data[i]
152 i++
153 j--
154
155 for {
156 for i <= j && cmp.Less(data[i], data[a]) {
157 i++
158 }
159 for i <= j && !cmp.Less(data[j], data[a]) {
160 j--
161 }
162 if i > j {
163 break
164 }
165 data[i], data[j] = data[j], data[i]
166 i++
167 j--
168 }
169 data[j], data[a] = data[a], data[j]
170 return j, false
171 }
172
173
174
175 func partitionEqualOrdered[E cmp.Ordered](data []E, a, b, pivot int) (newpivot int) {
176 data[a], data[pivot] = data[pivot], data[a]
177 i, j := a+1, b-1
178
179 for {
180 for i <= j && !cmp.Less(data[a], data[i]) {
181 i++
182 }
183 for i <= j && cmp.Less(data[a], data[j]) {
184 j--
185 }
186 if i > j {
187 break
188 }
189 data[i], data[j] = data[j], data[i]
190 i++
191 j--
192 }
193 return i
194 }
195
196
197 func partialInsertionSortOrdered[E cmp.Ordered](data []E, a, b int) bool {
198 const (
199 maxSteps = 5
200 shortestShifting = 50
201 )
202 i := a + 1
203 for j := 0; j < maxSteps; j++ {
204 for i < b && !cmp.Less(data[i], data[i-1]) {
205 i++
206 }
207
208 if i == b {
209 return true
210 }
211
212 if b-a < shortestShifting {
213 return false
214 }
215
216 data[i], data[i-1] = data[i-1], data[i]
217
218
219 if i-a >= 2 {
220 for j := i - 1; j >= 1; j-- {
221 if !cmp.Less(data[j], data[j-1]) {
222 break
223 }
224 data[j], data[j-1] = data[j-1], data[j]
225 }
226 }
227
228 if b-i >= 2 {
229 for j := i + 1; j < b; j++ {
230 if !cmp.Less(data[j], data[j-1]) {
231 break
232 }
233 data[j], data[j-1] = data[j-1], data[j]
234 }
235 }
236 }
237 return false
238 }
239
240
241
242 func breakPatternsOrdered[E cmp.Ordered](data []E, a, b int) {
243 length := b - a
244 if length >= 8 {
245 random := xorshift(length)
246 modulus := nextPowerOfTwo(length)
247
248 for idx := a + (length/4)*2 - 1; idx <= a+(length/4)*2+1; idx++ {
249 other := int(uint(random.Next()) & (modulus - 1))
250 if other >= length {
251 other -= length
252 }
253 data[idx], data[a+other] = data[a+other], data[idx]
254 }
255 }
256 }
257
258
259
260
261
262
263 func choosePivotOrdered[E cmp.Ordered](data []E, a, b int) (pivot int, hint sortedHint) {
264 const (
265 shortestNinther = 50
266 maxSwaps = 4 * 3
267 )
268
269 l := b - a
270
271 var (
272 swaps int
273 i = a + l/4*1
274 j = a + l/4*2
275 k = a + l/4*3
276 )
277
278 if l >= 8 {
279 if l >= shortestNinther {
280
281 i = medianAdjacentOrdered(data, i, &swaps)
282 j = medianAdjacentOrdered(data, j, &swaps)
283 k = medianAdjacentOrdered(data, k, &swaps)
284 }
285
286 j = medianOrdered(data, i, j, k, &swaps)
287 }
288
289 switch swaps {
290 case 0:
291 return j, increasingHint
292 case maxSwaps:
293 return j, decreasingHint
294 default:
295 return j, unknownHint
296 }
297 }
298
299
300 func order2Ordered[E cmp.Ordered](data []E, a, b int, swaps *int) (int, int) {
301 if cmp.Less(data[b], data[a]) {
302 *swaps++
303 return b, a
304 }
305 return a, b
306 }
307
308
309 func medianOrdered[E cmp.Ordered](data []E, a, b, c int, swaps *int) int {
310 a, b = order2Ordered(data, a, b, swaps)
311 b, c = order2Ordered(data, b, c, swaps)
312 a, b = order2Ordered(data, a, b, swaps)
313 return b
314 }
315
316
317 func medianAdjacentOrdered[E cmp.Ordered](data []E, a int, swaps *int) int {
318 return medianOrdered(data, a-1, a, a+1, swaps)
319 }
320
321 func reverseRangeOrdered[E cmp.Ordered](data []E, a, b int) {
322 i := a
323 j := b - 1
324 for i < j {
325 data[i], data[j] = data[j], data[i]
326 i++
327 j--
328 }
329 }
330
331 func swapRangeOrdered[E cmp.Ordered](data []E, a, b, n int) {
332 for i := 0; i < n; i++ {
333 data[a+i], data[b+i] = data[b+i], data[a+i]
334 }
335 }
336
337 func stableOrdered[E cmp.Ordered](data []E, n int) {
338 blockSize := 20
339 a, b := 0, blockSize
340 for b <= n {
341 insertionSortOrdered(data, a, b)
342 a = b
343 b += blockSize
344 }
345 insertionSortOrdered(data, a, n)
346
347 for blockSize < n {
348 a, b = 0, 2*blockSize
349 for b <= n {
350 symMergeOrdered(data, a, a+blockSize, b)
351 a = b
352 b += 2 * blockSize
353 }
354 if m := a + blockSize; m < n {
355 symMergeOrdered(data, a, m, n)
356 }
357 blockSize *= 2
358 }
359 }
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380 func symMergeOrdered[E cmp.Ordered](data []E, a, m, b int) {
381
382
383
384 if m-a == 1 {
385
386
387
388 i := m
389 j := b
390 for i < j {
391 h := int(uint(i+j) >> 1)
392 if cmp.Less(data[h], data[a]) {
393 i = h + 1
394 } else {
395 j = h
396 }
397 }
398
399 for k := a; k < i-1; k++ {
400 data[k], data[k+1] = data[k+1], data[k]
401 }
402 return
403 }
404
405
406
407
408 if b-m == 1 {
409
410
411
412 i := a
413 j := m
414 for i < j {
415 h := int(uint(i+j) >> 1)
416 if !cmp.Less(data[m], data[h]) {
417 i = h + 1
418 } else {
419 j = h
420 }
421 }
422
423 for k := m; k > i; k-- {
424 data[k], data[k-1] = data[k-1], data[k]
425 }
426 return
427 }
428
429 mid := int(uint(a+b) >> 1)
430 n := mid + m
431 var start, r int
432 if m > mid {
433 start = n - b
434 r = mid
435 } else {
436 start = a
437 r = m
438 }
439 p := n - 1
440
441 for start < r {
442 c := int(uint(start+r) >> 1)
443 if !cmp.Less(data[p-c], data[c]) {
444 start = c + 1
445 } else {
446 r = c
447 }
448 }
449
450 end := n - start
451 if start < m && m < end {
452 rotateOrdered(data, start, m, end)
453 }
454 if a < start && start < mid {
455 symMergeOrdered(data, a, start, mid)
456 }
457 if mid < end && end < b {
458 symMergeOrdered(data, mid, end, b)
459 }
460 }
461
462
463
464
465
466 func rotateOrdered[E cmp.Ordered](data []E, a, m, b int) {
467 i := m - a
468 j := b - m
469
470 for i != j {
471 if i > j {
472 swapRangeOrdered(data, m-i, m, j)
473 i -= j
474 } else {
475 swapRangeOrdered(data, m-i, m+j-i, i)
476 j -= i
477 }
478 }
479
480 swapRangeOrdered(data, m-i, m, i)
481 }
482
View as plain text