Skip to content

Commit b879f13

Browse files
authored
Using Base.Sort.InitialOptimizations (#70)
* Use initial optimizations but don't apply initial optimizations to radix sort twice * Better comment on a test
1 parent 051aad6 commit b879f13

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/SortingAlgorithms.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,13 @@ struct TimSortAlg <: Algorithm end
1616
struct RadixSortAlg <: Algorithm end
1717
struct CombSortAlg <: Algorithm end
1818

19-
const HeapSort = HeapSortAlg()
20-
const TimSort = TimSortAlg()
19+
function maybe_optimize(x::Algorithm)
20+
isdefined(Base.Sort, :InitialOptimizations) ? Base.Sort.InitialOptimizations(x) : x
21+
end
22+
const HeapSort = maybe_optimize(HeapSortAlg())
23+
const TimSort = maybe_optimize(TimSortAlg())
24+
# Whenever InitialOptimizations is defined, RadixSort falls
25+
# back to Base.DEFAULT_STABLE which already incldues them.
2126
const RadixSort = RadixSortAlg()
2227

2328
"""
@@ -44,7 +49,7 @@ Characteristics:
4449
- Werneck, N. L., (2020). "ChipSort: a SIMD and cache-aware sorting module. JuliaCon Proceedings, 1(1), 12, https://doi.org/10.21105/jcon.00012
4550
- 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.
4651
"""
47-
const CombSort = CombSortAlg()
52+
const CombSort = maybe_optimize(CombSortAlg())
4853

4954

5055
## Heap sort

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ for n in [0:10..., 100, 101, 1000, 1001]
103103
# test float sorting with NaNs
104104
s = sort(v, alg=alg, order=ord)
105105
@test issorted(s, order=ord)
106+
107+
# This tests that NaNs (which compare equivalent) are treated stably
108+
# even when the underlying algorithm is unstable. That it happens to
109+
# pass is not a part of the public API:
106110
@test reinterpret(UInt64, v[map(isnan, v)]) == reinterpret(UInt64, s[map(isnan, s)])
107111

108112
# test float permutation with NaNs

0 commit comments

Comments
 (0)