-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Clean up the pivot selection for QuickSort #16583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The partition function could also be improved for the case in which there is a considerable number of duplicates in the array. But that would need to use (and to have) both |
Cool. This looks like a perfect use case for the nanosoldier sorting benchmarks! |
Ok, I'll give it a try. @nanosoldier |
You need to be a contributor for it to work I think. |
I see. Then I leave to the appropriate party. |
|
Also need to cc nanosoldier. :) |
@nanosoldier |
Your benchmark job has completed - no performance regressions were detected. A full report can be found here. cc @jrevels |
I believe I benchmarked something wrong the first time. It seems now like a insignificant improvement and more like a cosmetic change. For the following function I get function test(v, n)
o = Base.Order.Forward
pivot = zero(eltype(v))
for i = 1:n
i, j = minmax(rand(1:length(v)), rand(1:length(v)))
pivot = selectpivot!(v, i, j, o)
end
return pivot
end
v = rand(10^7);
@benchmark test(v, 10^7)
Trial(1.68 s) # master
Trial(1.67 s) # this PR
Trial(1.70 s) # 0.4.5
Trial(1.69 s) # 0.4.5 + this change Anyway, I still believe the |
@pabloferz, it would be great if you coded up the version of quicksort with a three way partition and submitted a pull request to SortingAlgorithms.jl. |
This is and improvement to the current
selectpivot!
, that selects the pivot for the partition in theQuickSort
algorithm.Currently the method always orders the three elements and then swaps the first and middle elements. This change saves a little by ordering in such a way that
v[hi]
is the greatest andv[mi]
the smallest, so there is no need of a swapping at the end.For comparison the current implementation generates the following machine code
while the change produces
This seems to give near a 10% speed up for
sort
andsortperm
in some tests I made.CC @StefanKarpinski