-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathSortingAlgorithms.jl
690 lines (599 loc) · 20.1 KB
/
SortingAlgorithms.jl
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
__precompile__()
module SortingAlgorithms
using DataStructures
using Base.Sort
using Base.Order
import Base.Sort: sort!
import DataStructures: heapify!, percolate_down!
export HeapSort, TimSort, RadixSort, CombSort, BitonicSort
struct HeapSortAlg <: Algorithm end
struct TimSortAlg <: Algorithm end
struct RadixSortAlg <: Algorithm end
struct CombSortAlg <: Algorithm end
struct BitonicSortAlg <: Algorithm end
const HeapSort = HeapSortAlg()
const TimSort = TimSortAlg()
const RadixSort = RadixSortAlg()
"""
CombSort
Indicates that a sorting function should use the comb sort
algorithm. Comb sort traverses the collection multiple times
ordering pairs of elements with a given interval between them.
The interval decreases exponentially until it becomes 1, then
it switches to insertion sort on the whole input.
Characteristics:
- *not stable* does not preserve the ordering of elements which
compare equal (e.g. "a" and "A" in a sort of letters which
ignores case).
- *in-place* in memory.
- *parallelizable* suitable for vectorization with SIMD instructions
because it performs many independent comparisons.
- *pathological inputs* such as `repeat(1:5.0, 4^8)` can make this algorithm perform very poorly.
- *`n log n` average runtime* measured for random inputs of length up to 100 million, but theoretical runtime of `Θ(n^2)` for extremely long inputs.
## References
- Dobosiewicz, Wlodzimierz, (1980). "An efficient variation of bubble sort", Information Processing Letters, 11(1), pp. 5-6, https://doi.org/10.1016/0020-0190(80)90022-8.
- Werneck, N. L., (2020). "ChipSort: a SIMD and cache-aware sorting module. JuliaCon Proceedings, 1(1), 12, https://doi.org/10.21105/jcon.00012
- H. Inoue, T. Moriyama, H. Komatsu and T. Nakatani, "AA-Sort: A New Parallel Sorting Algorithm for Multi-Core SIMD Processors," 16th International Conference on Parallel Architecture and Compilation Techniques (PACT 2007), 2007, pp. 189-198, doi: 10.1109/PACT.2007.4336211.
"""
const CombSort = CombSortAlg()
"""
BitonicSort
Indicates that a sorting function should use the bitonic mergesort algorithm.
This algorithm performs a series of pre-determined comparisons, and tends to be very parallelizable.
The algorithm effectively implements a sorting network based on merging bitonic sequences.
Characteristics:
- *not stable* does not preserve the ordering of elements which
compare equal (e.g. "a" and "A" in a sort of letters which
ignores case).
- *in-place* in memory.
- *parallelizable* suitable for vectorization with SIMD instructions because
it performs many independent comparisons.
See wikipedia.org/wiki/Bitonic_sorter for more information.
"""
const BitonicSort = BitonicSortAlg()
## Heap sort
function sort!(v::AbstractVector, lo::Int, hi::Int, a::HeapSortAlg, o::Ordering)
if lo > 1 || hi < length(v)
return sort!(view(v, lo:hi), 1, length(v), a, o)
end
r = ReverseOrdering(o)
heapify!(v, r)
for i = length(v):-1:2
# Swap the root with i, the last unsorted position
x = v[i]
v[i] = v[1]
# The heap portion now ends at position i-1, but needs fixing up
# starting with the root
percolate_down!(v,1,x,r,i-1)
end
v
end
## Radix sort
# Map a bits-type to an unsigned int, maintaining sort order
uint_mapping(::ForwardOrdering, x::Unsigned) = x
for (signedty, unsignedty) in ((Int8, UInt8), (Int16, UInt16), (Int32, UInt32), (Int64, UInt64), (Int128, UInt128))
# In Julia 0.4 we can just use unsigned() here
@eval uint_mapping(::ForwardOrdering, x::$signedty) = reinterpret($unsignedty, xor(x, typemin(typeof(x))))
end
uint_mapping(::ForwardOrdering, x::Float32) = (y = reinterpret(Int32, x); reinterpret(UInt32, ifelse(y < 0, ~y, xor(y, typemin(Int32)))))
uint_mapping(::ForwardOrdering, x::Float64) = (y = reinterpret(Int64, x); reinterpret(UInt64, ifelse(y < 0, ~y, xor(y, typemin(Int64)))))
uint_mapping(::Sort.Float.Left, x::Float16) = ~reinterpret(Int16, x)
uint_mapping(::Sort.Float.Right, x::Float16) = reinterpret(Int16, x)
uint_mapping(::Sort.Float.Left, x::Float32) = ~reinterpret(Int32, x)
uint_mapping(::Sort.Float.Right, x::Float32) = reinterpret(Int32, x)
uint_mapping(::Sort.Float.Left, x::Float64) = ~reinterpret(Int64, x)
uint_mapping(::Sort.Float.Right, x::Float64) = reinterpret(Int64, x)
uint_mapping(rev::ReverseOrdering, x) = ~uint_mapping(rev.fwd, x)
uint_mapping(::ReverseOrdering{ForwardOrdering}, x::Real) = ~uint_mapping(Forward, x) # maybe unnecessary; needs benchmark
uint_mapping(o::By, x ) = uint_mapping(Forward, o.by(x))
uint_mapping(o::Perm, i::Int) = uint_mapping(o.order, o.data[i])
uint_mapping(o::Lt, x ) = error("uint_mapping does not work with general Lt Orderings")
const RADIX_SIZE = 11
const RADIX_MASK = 0x7FF
function sort!(vs::AbstractVector{T}, lo::Int, hi::Int, ::RadixSortAlg, o::Ordering, ts::AbstractVector{T}=similar(vs)) where T
# Input checking
if lo >= hi; return vs; end
# Make sure we're sorting a bits type
OT = Base.Order.ordtype(o, vs)
if !isbitstype(OT)
error("Radix sort only sorts bits types (got $OT)")
end
# Init
iters = ceil(Integer, sizeof(OT)*8/RADIX_SIZE)
bin = zeros(UInt32, 2^RADIX_SIZE, iters)
if lo > 1; bin[1,:] .= lo-1; end
# Histogram for each element, radix
for i = lo:hi
v = uint_mapping(o, vs[i])
for j = 1:iters
idx = Int((v >> ((j-1)*RADIX_SIZE)) & RADIX_MASK) + 1
@inbounds bin[idx,j] += 1
end
end
# Sort!
swaps = 0
len = hi-lo+1
for j = 1:iters
# Unroll first data iteration, check for degenerate case
v = uint_mapping(o, vs[hi])
idx = Int((v >> ((j-1)*RADIX_SIZE)) & RADIX_MASK) + 1
# are all values the same at this radix?
if bin[idx,j] == len; continue; end
cbin = cumsum(bin[:,j])
ci = cbin[idx]
ts[ci] = vs[hi]
cbin[idx] -= 1
# Finish the loop...
@inbounds for i in hi-1:-1:lo
v = uint_mapping(o, vs[i])
idx = Int((v >> ((j-1)*RADIX_SIZE)) & RADIX_MASK) + 1
ci = cbin[idx]
ts[ci] = vs[i]
cbin[idx] -= 1
end
vs,ts = ts,vs
swaps += 1
end
if isodd(swaps)
vs,ts = ts,vs
for i = lo:hi
vs[i] = ts[i]
end
end
vs
end
function Base.Sort.Float.fpsort!(v::AbstractVector, ::RadixSortAlg, o::Ordering)
@static if VERSION >= v"1.7.0-DEV"
lo, hi = Base.Sort.Float.specials2end!(v, RadixSort, o)
else
lo, hi = Base.Sort.Float.nans2end!(v, o)
end
sort!(v, lo, hi, RadixSort, o)
end
# Implementation of TimSort based on the algorithm description at:
#
# http://svn.python.org/projects/python/trunk/Objects/listsort.txt
# http://en.wikipedia.org/wiki/Timsort
#
# Original author: @kmsquire
const Run = UnitRange{Int}
const MIN_GALLOP = 7
mutable struct MergeState
runs::Vector{Run}
min_gallop::Int
end
MergeState() = MergeState(Run[], MIN_GALLOP)
# Determine a good minimum run size for efficient merging
# For details, see "Computing minrun" in
# http://svn.python.org/projects/python/trunk/Objects/listsort.txt
function merge_compute_minrun(N::Int, bits::Int)
r = 0
max_val = 2^bits
while N >= max_val
r |= (N & 1)
N >>= 1
end
N + r
end
merge_compute_minrun(N::Int) = merge_compute_minrun(N, 6)
# Galloping binary search starting at left
# Finds the last value in v <= x
function gallop_last(o::Ordering, v::AbstractVector, x, lo::Int, hi::Int)
i = lo
inc = 1
lo = lo-1
hi = hi+1
while i < hi && !lt(o, x, v[i])
lo = i
i += inc
inc <<= 1
end
hi = min(i+1, hi)
# Binary search
while lo < hi-1
i = (lo+hi)>>>1
if lt(o, x, v[i])
hi = i
else
lo = i
end
end
lo
end
# Galloping binary search starting at right
# Finds the last value in v <= x
function rgallop_last(o::Ordering, v::AbstractVector, x, lo::Int, hi::Int)
i = hi
dec = 1
lo = lo-1
hi = hi+1
while i > lo && lt(o, x, v[i])
hi = i
i -= dec
dec <<= 1
end
lo = max(lo, i-1)
# Binary search
while lo < hi-1
i = (lo+hi)>>>1
if lt(o, x, v[i])
hi = i
else
lo = i
end
end
lo
end
# Galloping binary search starting at left
# Finds the first value in v >= x
function gallop_first(o::Ordering, v::AbstractVector, x, lo::Int, hi::Int)
i = lo
inc = 1
lo = lo-1
hi = hi+1
while i < hi && lt(o, v[i], x)
lo = i
i += inc
inc <<= 1
end
hi = min(i+1, hi)
# Binary search
while lo < hi-1
i = (lo+hi)>>>1
if lt(o, v[i], x)
lo = i
else
hi = i
end
end
hi
end
# Galloping binary search starting at right
# Finds the first value in v >= x
function rgallop_first(o::Ordering, v::AbstractVector, x, lo::Int, hi::Int)
i = hi
dec = 1
lo = lo-1
hi = hi+1
while i > lo && !lt(o, v[i], x)
hi = i
i -= dec
dec <<= 1
end
lo = max(lo, i-1)
# Binary search
while lo < hi-1
i = (lo+hi)>>>1
if lt(o, v[i], x)
lo = i
else
hi = i
end
end
hi
end
# Get the next run
# Returns the v range a:b, or b:-1:a for a reversed sequence
function next_run(o::Ordering, v::AbstractVector, lo::Int, hi::Int)
lo == hi && return lo:hi
if !lt(o, v[lo+1], v[lo])
for i = lo+2:hi
if lt(o, v[i], v[i-1])
return lo:i-1
end
end
return lo:hi
else
for i = lo+2:hi
if !lt(o, v[i], v[i-1])
return i-1:-1:lo
end
end
return hi:-1:lo
end
end
function merge_at(o::Ordering, v::AbstractVector, state::MergeState, n::Integer)
a = state.runs[n]
b = state.runs[n+1]
merge(o,v,a,b,state)
state.runs[n] = first(a):last(b)
deleteat!(state.runs, n+1)
nothing
end
# Merge consecutive runs
# For A,B,C,D = last four lengths, merge_collapse!()
# maintains 3 invariants:
#
# A > B + C
# B > C + D
# C > D
#
# If any of these are violated, a merge occurs to
# correct it
function merge_collapse(o::Ordering, v::AbstractVector, state::MergeState)
while true
n = length(state.runs)
n <= 1 && break
# Check invariants 1 and 2
if (n >= 3 && length(state.runs[end-2]) <= length(state.runs[end-1]) + length(state.runs[end])) ||
(n >= 4 && length(state.runs[end-3]) <= length(state.runs[end-2]) + length(state.runs[end-1]))
if length(state.runs[end-2]) < length(state.runs[end])
merge_at(o,v,state,n-2)
else
merge_at(o,v,state,n-1)
end
# Check invariant 3
elseif length(state.runs[end-1]) <= length(state.runs[end])
merge_at(o,v,state,n-1)
else # Invariant is satisfied
break
end
end
end
# Merge runs a and b in vector v
function merge(o::Ordering, v::AbstractVector, a::Run, b::Run, state::MergeState)
# First elements in a <= b[1] are already in place
a = gallop_last(o, v, v[first(b)], first(a), last(a))+1: last(a)
if length(a) == 0 return end
# Last elements in b >= a[end] are already in place
b = first(b) : rgallop_first(o, v, v[last(a)], first(b), last(b))-1
# Choose merge_lo or merge_hi based on the amount
# of temporary memory needed (smaller of a and b)
if length(a) < length(b)
merge_lo(o, v, a, b, state)
else
merge_hi(o, v, a, b, state)
end
end
# Merge runs a and b in vector v (a is smaller)
function merge_lo(o::Ordering, v::AbstractVector, a::Run, b::Run, state::MergeState)
# Copy a
v_a = v[a]
# Pointer into (sub)arrays
i = first(a)
from_a = 1
from_b = first(b)
mode = :normal
while true
if mode == :normal
# Compare and copy element by element
count_a = count_b = 0
while from_a <= length(a) && from_b <= last(b)
if lt(o, v[from_b], v_a[from_a])
v[i] = v[from_b]
from_b += 1
count_a = 0
count_b += 1
else
v[i] = v_a[from_a]
from_a += 1
count_a += 1
count_b = 0
end
i += 1
# Switch to galloping mode if a string of elements
# has come from the same set
if count_b >= state.min_gallop || count_a >= state.min_gallop
mode = :galloping
break
end
end
# Finalize if we've exited the loop normally
if mode == :normal
mode = :finalize
end
end
if mode == :galloping
# Use binary search to find range to copy
while from_a <= length(a) && from_b <= last(b)
# Copy the next run from b
b_run = from_b : gallop_first(o, v, v_a[from_a], from_b, last(b)) - 1
i_end = i + length(b_run) - 1
v[i:i_end] = v[b_run]
i = i_end + 1
from_b = last(b_run) + 1
# ... then copy the first element from a
v[i] = v_a[from_a]
i += 1
from_a += 1
if from_a > length(a) || from_b > last(b) break end
# Copy the next run from a
a_run = from_a : gallop_last(o, v_a, v[from_b], from_a, length(a))
i_end = i + length(a_run) - 1
v[i:i_end] = v_a[a_run]
i = i_end + 1
from_a = last(a_run) + 1
# ... then copy the first element from b
v[i] = v[from_b]
i += 1
from_b += 1
# Return to normal mode if we haven't galloped...
if length(a_run) < MIN_GALLOP && length(b_run) < MIN_GALLOP
mode = :normal
break
end
# Reduce min_gallop if this gallop was successful
state.min_gallop -= 1
end
if mode == :galloping
mode = :finalize
end
state.min_gallop = max(state.min_gallop,0) + 2 # penalty for leaving gallop mode
end
if mode == :finalize
# copy end of a
i_end = i + (length(a) - from_a)
v[i:i_end] = v_a[from_a:end]
break
end
end
end
# Merge runs a and b in vector v (b is smaller)
function merge_hi(o::Ordering, v::AbstractVector, a::Run, b::Run, state::MergeState)
# Copy b
v_b = v[b]
# Pointer into (sub)arrays
i = last(b)
from_a = last(a)
from_b = length(b)
mode = :normal
while true
if mode == :normal
# Compare and copy element by element
count_a = count_b = 0
while from_a >= first(a) && from_b >= 1
if !lt(o, v_b[from_b], v[from_a])
v[i] = v_b[from_b]
from_b -= 1
count_a = 0
count_b += 1
else
v[i] = v[from_a]
from_a -= 1
count_a += 1
count_b = 0
end
i -= 1
# Switch to galloping mode if a string of elements
# has come from the same set
if count_b >= state.min_gallop || count_a >= state.min_gallop
mode = :galloping
break
end
end
# Finalize if we've exited the loop normally
if mode == :normal
mode = :finalize
end
end
if mode == :galloping
# Use binary search to find range to copy
while from_a >= first(a) && from_b >= 1
# Copy the next run from b
b_run = rgallop_first(o, v_b, v[from_a], 1, from_b) : from_b
i_start = i - length(b_run) + 1
v[i_start:i] = v_b[b_run]
i = i_start - 1
from_b = first(b_run) - 1
# ... then copy the first element from a
v[i] = v[from_a]
i -= 1
from_a -= 1
if from_a < first(a) || from_b < 1 break end
# Copy the next run from a
a_run = rgallop_last(o, v, v_b[from_b], first(a), from_a) + 1: from_a
i_start = i - length(a_run) + 1
v[i_start:i] = v[a_run]
i = i_start - 1
from_a = first(a_run) - 1
# ... then copy the first element from b
v[i] = v_b[from_b]
i -= 1
from_b -= 1
# Return to normal mode if we haven't galloped...
if length(a_run) < MIN_GALLOP && length(b_run) < MIN_GALLOP
mode = :normal
break
end
# Reduce min_gallop if this gallop was successful
state.min_gallop -= 1
end
if mode == :galloping
mode = :finalize
end
state.min_gallop = max(state.min_gallop, 0) + 2 # penalty for leaving gallop mode
end
if mode == :finalize
# copy start of b
i_start = i - from_b + 1
v[i_start:i] = v_b[1:from_b]
break
end
end
end
# TimSort main method
function sort!(v::AbstractVector, lo::Int, hi::Int, ::TimSortAlg, o::Ordering)
minrun = merge_compute_minrun(hi-lo+1)
state = MergeState()
i = lo
while i <= hi
run_range = next_run(o, v, i, hi)
count = length(run_range)
if count < minrun
# Make a run of length minrun
count = min(minrun, hi-i+1)
run_range = i:i+count-1
sort!(v, i, i+count-1, DEFAULT_STABLE, o)
else
if !issorted(run_range)
run_range = last(run_range):first(run_range)
reverse!(view(v, run_range))
end
end
# Push this run onto the queue and merge if needed
push!(state.runs, run_range)
i = i+count
merge_collapse(o, v, state)
end
# Force merge at the end
while true
n = length(state.runs)
n <= 1 && break
merge_at(o, v, state, n-1)
end
return v
end
function sort!(v::AbstractVector, lo::Int, hi::Int, ::CombSortAlg, o::Ordering)
interval = (3 * (hi-lo+1)) >> 2
while interval > 1
for j in lo:hi-interval
@inbounds comparator!(v, j, j+interval, o)
end
interval = (3 * interval) >> 2
end
return sort!(v, lo, hi, InsertionSort, o)
end
function sort!(v::AbstractVector, lo::Int, hi::Int, ::BitonicSortAlg, o::Ordering)
return bitonicsort!(view(v, lo:hi), o::Ordering)
end
function bitonicsort!(data, o::Ordering)
N = length(data)
for n in 1:intlog2(N)
bitonicfirststage!(data, Val(n), o::Ordering)
for m in n-1:-1:1
bitonicsecondstage!(data, Val(m), o::Ordering)
end
end
return data
end
function bitonicfirststage!(v, ::Val{Gap}, o::Ordering) where Gap
N = length(v)
gap = 1 << Gap
halfgap = 1 << (Gap - 1)
for i in 0:gap:N-1
firstj = max(0, i + gap - N)
for j in firstj:(halfgap-1)
ia = i + j
ib = i + gap - j - 1
@inbounds comparator!(v, ia + 1, ib + 1, o)
end
end
end
function bitonicsecondstage!(v, ::Val{Gap}, o::Ordering) where Gap
N = length(v)
gap = 1 << Gap
for i in 0:gap:N-1
lastj = min(N - 1, N - (i + gap >> 1 + 1))
for j in 0:lastj
ia = i + j
ib = i + j + gap >> 1
@inbounds comparator!(v, ia + 1, ib + 1, o)
end
end
end
intlog2(n) = (n > 1) ? 8sizeof(n-1)-leading_zeros(n-1) : 0
Base.@propagate_inbounds function comparator!(v, i, j, o)
a, b = v[i], v[j]
v[i], v[j] = lt(o, b, a) ? (b, a) : (a, b)
end
end # module