Skip to content

Commit

Permalink
Use middle element in quicksort [fixes #681].
Browse files Browse the repository at this point in the history
This leaves us susceptible to O(n^2) worst-case quicksort behavior
if the array to be sorted has a malicious or unfortunate ordering.
  • Loading branch information
StefanKarpinski committed Apr 11, 2012
1 parent 9e03fff commit e6877a8
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ function ($quicksort)($(args...), a::AbstractVector, lo::Int, hi::Int)
end
i, j = lo, hi
# pivot = (a[lo]+a[hi])/2 # 1.14x
# pivot = a[(lo+hi)>>>1] # 1.15x
pivot = (a[lo]+a[hi]+a[(lo+hi)>>>1])/3 # 1.16x
pivot = a[(lo+hi)>>>1] # 1.15x
# pivot = (a[lo]+a[hi]+a[(lo+hi)>>>1])/3 # 1.16x
# pivot = _jl_pivot_middle(a[lo], a[hi], a[(lo+hi)>>>1]) # 1.23x
# pivot = a[randival(lo,hi)] # 1.28x
while i <= j
Expand Down

0 comments on commit e6877a8

Please sign in to comment.