Skip to content

Commit

Permalink
rename QuickerSort to ScratchQuickSort (#48160)
Browse files Browse the repository at this point in the history
  • Loading branch information
LilithHafner authored Jan 13, 2023
1 parent eb5f6d6 commit 9707594
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
34 changes: 17 additions & 17 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ issorted(itr;
issorted(itr, ord(lt,by,rev,order))

function partialsort!(v::AbstractVector, k::Union{Integer,OrdinalRange}, o::Ordering)
_sort!(v, InitialOptimizations(QuickerSort(k)), o, (;))
_sort!(v, InitialOptimizations(ScratchQuickSort(k)), o, (;))
maybeview(v, k)
end

Expand Down Expand Up @@ -950,12 +950,12 @@ end


"""
QuickerSort(next::Algorithm=SMALL_ALGORITHM) <: Algorithm
QuickerSort(lo::Union{Integer, Missing}, hi::Union{Integer, Missing}=lo, next::Algorithm=SMALL_ALGORITHM) <: Algorithm
ScratchQuickSort(next::Algorithm=SMALL_ALGORITHM) <: Algorithm
ScratchQuickSort(lo::Union{Integer, Missing}, hi::Union{Integer, Missing}=lo, next::Algorithm=SMALL_ALGORITHM) <: Algorithm
Use the `QuickerSort` algorithm with the `next` algorithm as a base case.
Use the `ScratchQuickSort` algorithm with the `next` algorithm as a base case.
`QuickerSort` is like `QuickSort`, but utilizes scratch space to operate faster and allow
`ScratchQuickSort` is like `QuickSort`, but utilizes scratch space to operate faster and allow
for the possibility of maintaining stability.
If `lo` and `hi` are provided, finds and sorts the elements in the range `lo:hi`, reordering
Expand All @@ -973,15 +973,15 @@ Characteristics:
* *quadratic worst case runtime* in pathological cases
(vanishingly rare for non-malicious input)
"""
struct QuickerSort{L<:Union{Integer,Missing}, H<:Union{Integer,Missing}, T<:Algorithm} <: Algorithm
struct ScratchQuickSort{L<:Union{Integer,Missing}, H<:Union{Integer,Missing}, T<:Algorithm} <: Algorithm
lo::L
hi::H
next::T
end
QuickerSort(next::Algorithm=SMALL_ALGORITHM) = QuickerSort(missing, missing, next)
QuickerSort(lo::Union{Integer, Missing}, hi::Union{Integer, Missing}) = QuickerSort(lo, hi, SMALL_ALGORITHM)
QuickerSort(lo::Union{Integer, Missing}, next::Algorithm=SMALL_ALGORITHM) = QuickerSort(lo, lo, next)
QuickerSort(r::OrdinalRange, next::Algorithm=SMALL_ALGORITHM) = QuickerSort(first(r), last(r), next)
ScratchQuickSort(next::Algorithm=SMALL_ALGORITHM) = ScratchQuickSort(missing, missing, next)
ScratchQuickSort(lo::Union{Integer, Missing}, hi::Union{Integer, Missing}) = ScratchQuickSort(lo, hi, SMALL_ALGORITHM)
ScratchQuickSort(lo::Union{Integer, Missing}, next::Algorithm=SMALL_ALGORITHM) = ScratchQuickSort(lo, lo, next)
ScratchQuickSort(r::OrdinalRange, next::Algorithm=SMALL_ALGORITHM) = ScratchQuickSort(first(r), last(r), next)

# select a pivot, partition v[lo:hi] according
# to the pivot, and store the result in t[lo:hi].
Expand Down Expand Up @@ -1020,7 +1020,7 @@ function partition!(t::AbstractVector, lo::Integer, hi::Integer, offset::Integer
pivot_index
end

function _sort!(v::AbstractVector, a::QuickerSort, o::Ordering, kw;
function _sort!(v::AbstractVector, a::ScratchQuickSort, o::Ordering, kw;
t=nothing, offset=nothing, swap=false, rev=false)
@getkw lo hi scratch

Expand All @@ -1038,7 +1038,7 @@ function _sort!(v::AbstractVector, a::QuickerSort, o::Ordering, kw;
end
swap = !swap

# For QuickerSort(), a.lo === a.hi === missing, so the first two branches get skipped
# For ScratchQuickSort(), a.lo === a.hi === missing, so the first two branches get skipped
if !ismissing(a.lo) && j <= a.lo # Skip sorting the lower part
swap && copyto!(v, lo, t, lo+offset, j-lo)
rev && reverse!(v, lo, j-1)
Expand Down Expand Up @@ -1236,7 +1236,7 @@ the initial optimizations because they can change the input vector's type and or
make them `UIntMappable`.
If the input is not [`UIntMappable`](@ref), then we perform a presorted check and dispatch
to [`QuickerSort`](@ref).
to [`ScratchQuickSort`](@ref).
Otherwise, we dispatch to [`InsertionSort`](@ref) for inputs with `length <= 40` and then
perform a presorted check ([`CheckSorted`](@ref)).
Expand Down Expand Up @@ -1268,7 +1268,7 @@ Consequently, we apply [`RadixSort`](@ref) for any reasonably long inputs that r
stage.
Finally, if the input has length less than 80, we dispatch to [`InsertionSort`](@ref) and
otherwise we dispatch to [`QuickerSort`](@ref).
otherwise we dispatch to [`ScratchQuickSort`](@ref).
"""
const DEFAULT_STABLE = InitialOptimizations(
IsUIntMappable(
Expand All @@ -1278,9 +1278,9 @@ const DEFAULT_STABLE = InitialOptimizations(
ConsiderCountingSort(
ConsiderRadixSort(
Small{80}(
QuickerSort())))))),
ScratchQuickSort())))))),
StableCheckSorted(
QuickerSort())))
ScratchQuickSort())))
"""
DEFAULT_UNSTABLE
Expand Down Expand Up @@ -1485,7 +1485,7 @@ function partialsortperm!(ix::AbstractVector{<:Integer}, v::AbstractVector,
end

# do partial quicksort
_sort!(ix, InitialOptimizations(QuickerSort(k)), Perm(ord(lt, by, rev, order), v), (;))
_sort!(ix, InitialOptimizations(ScratchQuickSort(k)), Perm(ord(lt, by, rev, order), v), (;))

maybeview(ix, k)
end
Expand Down
20 changes: 10 additions & 10 deletions test/sorting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ end
end

@testset "stability" begin
for Alg in [InsertionSort, MergeSort, Base.Sort.QuickerSort(), Base.DEFAULT_STABLE,
Base.Sort.QuickerSort(missing, 1729), Base.Sort.QuickerSort(1729, missing)]
for Alg in [InsertionSort, MergeSort, Base.Sort.ScratchQuickSort(), Base.DEFAULT_STABLE,
Base.Sort.ScratchQuickSort(missing, 1729), Base.Sort.ScratchQuickSort(1729, missing)]
@test issorted(sort(1:2000, alg=Alg, by=x->0))
@test issorted(sort(1:2000, alg=Alg, by=x->x÷100))
end
Expand Down Expand Up @@ -333,7 +333,7 @@ end
@test c == v

# stable algorithms
for alg in [MergeSort, Base.Sort.QuickerSort(), Base.Sort.QuickerSort(1:n), Base.DEFAULT_STABLE]
for alg in [MergeSort, Base.Sort.ScratchQuickSort(), Base.Sort.ScratchQuickSort(1:n), Base.DEFAULT_STABLE]
p = sortperm(v, alg=alg, rev=rev)
p2 = sortperm(float(v), alg=alg, rev=rev)
@test p == p2
Expand Down Expand Up @@ -381,7 +381,7 @@ end
end

v = randn_with_nans(n,0.1)
for alg in [InsertionSort, MergeSort, Base.Sort.QuickerSort(), Base.Sort.QuickerSort(1, n), Base.DEFAULT_UNSTABLE, Base.DEFAULT_STABLE],
for alg in [InsertionSort, MergeSort, Base.Sort.ScratchQuickSort(), Base.Sort.ScratchQuickSort(1, n), Base.DEFAULT_UNSTABLE, Base.DEFAULT_STABLE],
rev in [false,true]
alg === InsertionSort && n >= 3000 && continue
# test float sorting with NaNs
Expand Down Expand Up @@ -588,7 +588,7 @@ end

@testset "fallback" begin
@test adaptive_sort_test(rand(1:typemax(Int32), len), by=x->x^2)# fallback
@test adaptive_sort_test(rand(Int, len), by=x->0, trusted=Base.Sort.QuickerSort())
@test adaptive_sort_test(rand(Int, len), by=x->0, trusted=Base.Sort.ScratchQuickSort())
end

@test adaptive_sort_test(rand(Int, 20)) # InsertionSort
Expand Down Expand Up @@ -692,7 +692,7 @@ end
# not allowed. Consequently, none of the behavior tested in this
# testset is guaranteed to work in future minor versions of Julia.

safe_algs = [InsertionSort, MergeSort, Base.Sort.QuickerSort(), Base.DEFAULT_STABLE, Base.DEFAULT_UNSTABLE]
safe_algs = [InsertionSort, MergeSort, Base.Sort.ScratchQuickSort(), Base.DEFAULT_STABLE, Base.DEFAULT_UNSTABLE]

n = 1000
v = rand(1:5, n);
Expand Down Expand Up @@ -899,8 +899,8 @@ end
@test issorted(sort(rand(Int8, 600)))
end

@testset "QuickerSort API" begin
bsqs = Base.Sort.QuickerSort
@testset "ScratchQuickSort API" begin
bsqs = Base.Sort.ScratchQuickSort
@test bsqs(1, 2, MergeSort) === bsqs(1, 2, MergeSort)
@test bsqs(missing, 2, MergeSort) === bsqs(missing, 2, MergeSort)
@test bsqs(1, missing, MergeSort) === bsqs(1, missing, MergeSort)
Expand All @@ -918,10 +918,10 @@ end
@test bsqs() === bsqs(missing, missing, InsertionSort)
end

@testset "QuickerSort allocations on non-concrete eltype" begin
@testset "ScratchQuickSort allocations on non-concrete eltype" begin
v = Vector{Union{Nothing, Bool}}(rand(Bool, 10000))
@test 4 == @allocations sort(v)
@test 4 == @allocations sort(v; alg=Base.Sort.QuickerSort())
@test 4 == @allocations sort(v; alg=Base.Sort.ScratchQuickSort())
# it would be nice if these numbers were lower (1 or 2), but these
# test that we don't have O(n) allocations due to type instability
end
Expand Down

0 comments on commit 9707594

Please sign in to comment.