From 223ab868bddf72f4c33537e707e636e6ad757771 Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Mon, 17 Oct 2022 13:48:29 +0600 Subject: [PATCH 1/5] Reduce the range of elements sorted by partialsort --- base/sort.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/base/sort.jl b/base/sort.jl index 4259c9326496d..682ac81bd0634 100644 --- a/base/sort.jl +++ b/base/sort.jl @@ -434,7 +434,7 @@ struct PartialQuickSort{L<:Union{Integer,Missing}, H<:Union{Integer,Missing}} <: lo::L hi::H end -PartialQuickSort(k::Integer) = PartialQuickSort(missing, k) +PartialQuickSort(k::Integer) = PartialQuickSort(k, k) PartialQuickSort(k::OrdinalRange) = PartialQuickSort(first(k), last(k)) """ From 21185be6237b7110a56b23cefc6aaeaf4d0938ad Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Mon, 17 Oct 2022 15:08:52 +0600 Subject: [PATCH 2/5] Don't test that PartialQuickSort sorts the whole vector --- test/sorting.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/sorting.jl b/test/sorting.jl index acb628406581e..44b8399dfd40f 100644 --- a/test/sorting.jl +++ b/test/sorting.jl @@ -381,7 +381,7 @@ end end v = randn_with_nans(n,0.1) - for alg in [InsertionSort, MergeSort, QuickSort, PartialQuickSort(n), Base.DEFAULT_UNSTABLE, Base.DEFAULT_STABLE], + for alg in [InsertionSort, MergeSort, QuickSort, Base.DEFAULT_UNSTABLE, Base.DEFAULT_STABLE], rev in [false,true] alg === InsertionSort && n >= 3000 && continue # test float sorting with NaNs From 62532ea38d007c992810b159f3130208219bc750 Mon Sep 17 00:00:00 2001 From: Lilith Hafner Date: Mon, 17 Oct 2022 15:31:47 +0600 Subject: [PATCH 3/5] preserve backwards compatability (thanks @petvana) --- base/sort.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/base/sort.jl b/base/sort.jl index 682ac81bd0634..6b9f57bca699d 100644 --- a/base/sort.jl +++ b/base/sort.jl @@ -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, PartialQuickSort(k), o) + sort!(v, PartialQuickSort(k, k), o) maybeview(v, k) end @@ -434,7 +434,7 @@ struct PartialQuickSort{L<:Union{Integer,Missing}, H<:Union{Integer,Missing}} <: lo::L hi::H end -PartialQuickSort(k::Integer) = PartialQuickSort(k, k) +PartialQuickSort(k::Integer) = PartialQuickSort(missing, k) PartialQuickSort(k::OrdinalRange) = PartialQuickSort(first(k), last(k)) """ @@ -1082,7 +1082,7 @@ function partialsortperm!(ix::AbstractVector{<:Integer}, v::AbstractVector, end # do partial quicksort - sort!(ix, PartialQuickSort(k), Perm(ord(lt, by, rev, order), v)) + sort!(ix, PartialQuickSort(k, k), Perm(ord(lt, by, rev, order), v)) maybeview(ix, k) end From 91a59e4e5963b8a2b695fd50f6cc478d1a461a82 Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Mon, 17 Oct 2022 17:35:14 +0600 Subject: [PATCH 4/5] Revert loosening tests Co-authored-by: Petr Vana --- test/sorting.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/sorting.jl b/test/sorting.jl index 44b8399dfd40f..acb628406581e 100644 --- a/test/sorting.jl +++ b/test/sorting.jl @@ -381,7 +381,7 @@ end end v = randn_with_nans(n,0.1) - for alg in [InsertionSort, MergeSort, QuickSort, Base.DEFAULT_UNSTABLE, Base.DEFAULT_STABLE], + for alg in [InsertionSort, MergeSort, QuickSort, PartialQuickSort(n), Base.DEFAULT_UNSTABLE, Base.DEFAULT_STABLE], rev in [false,true] alg === InsertionSort && n >= 3000 && continue # test float sorting with NaNs From 325438a2d622f9760ec16d31af805864d75ca1d3 Mon Sep 17 00:00:00 2001 From: Lilith Orion Hafner Date: Sun, 23 Oct 2022 19:11:00 +0600 Subject: [PATCH 5/5] Switch approach slightly to fix bug --- base/sort.jl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/base/sort.jl b/base/sort.jl index 6b9f57bca699d..5cf64bffea118 100644 --- a/base/sort.jl +++ b/base/sort.jl @@ -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, PartialQuickSort(k, k), o) + sort!(v, _PartialQuickSort(k), o) maybeview(v, k) end @@ -436,6 +436,8 @@ struct PartialQuickSort{L<:Union{Integer,Missing}, H<:Union{Integer,Missing}} <: end PartialQuickSort(k::Integer) = PartialQuickSort(missing, k) PartialQuickSort(k::OrdinalRange) = PartialQuickSort(first(k), last(k)) +_PartialQuickSort(k::Integer) = PartialQuickSort(k, k) +_PartialQuickSort(k::OrdinalRange) = PartialQuickSort(k) """ InsertionSort @@ -1082,7 +1084,7 @@ function partialsortperm!(ix::AbstractVector{<:Integer}, v::AbstractVector, end # do partial quicksort - sort!(ix, PartialQuickSort(k, k), Perm(ord(lt, by, rev, order), v)) + sort!(ix, _PartialQuickSort(k), Perm(ord(lt, by, rev, order), v)) maybeview(ix, k) end