@@ -998,7 +998,8 @@ select_pivot(lo::Integer, hi::Integer) = typeof(hi-lo)(hash(lo) % (hi-lo+1)) + l
998
998
#
999
999
# returns (pivot, pivot_index) where pivot_index is the location the pivot
1000
1000
# should end up, but does not set t[pivot_index] = pivot
1001
- function partition! (t:: AbstractVector , lo:: Integer , hi:: Integer , offset:: Integer , o:: Ordering , v:: AbstractVector , rev:: Bool )
1001
+ function partition! (t:: AbstractVector , lo:: Integer , hi:: Integer , offset:: Integer , o:: Ordering ,
1002
+ v:: AbstractVector , rev:: Bool , pivot_dest:: AbstractVector , pivot_index_offset:: Integer )
1002
1003
pivot_index = select_pivot (lo, hi)
1003
1004
@inbounds begin
1004
1005
pivot = v[pivot_index]
@@ -1016,14 +1017,16 @@ function partition!(t::AbstractVector, lo::Integer, hi::Integer, offset::Integer
1016
1017
offset += fx
1017
1018
lo += 1
1018
1019
end
1020
+ pivot_index = lo- offset + pivot_index_offset
1021
+ pivot_dest[pivot_index] = pivot
1019
1022
end
1020
1023
1021
- # pivot_index = lo-offset
1022
- # t[pivot_index ] is whatever it was before
1023
- # t[<pivot_index ] <* pivot, stable
1024
- # t[>pivot_index ] >* pivot, reverse stable
1024
+ # t_pivot_index = lo-offset (i.e. without pivot_index_offset)
1025
+ # t[t_pivot_index ] is whatever it was before unless t is the pivot_dest
1026
+ # t[<t_pivot_index ] <* pivot, stable
1027
+ # t[>t_pivot_index ] >* pivot, reverse stable
1025
1028
1026
- pivot, lo - offset
1029
+ pivot_index
1027
1030
end
1028
1031
1029
1032
function _sort! (v:: AbstractVector , a:: QuickerSort , o:: Ordering , kw;
@@ -1037,9 +1040,11 @@ function _sort!(v::AbstractVector, a::QuickerSort, o::Ordering, kw;
1037
1040
end
1038
1041
1039
1042
while lo < hi && hi - lo > SMALL_THRESHOLD
1040
- pivot, j = swap ? partition! (v, lo+ offset, hi+ offset, offset, o, t, rev) : partition! (t, lo, hi, - offset, o, v, rev)
1041
- j -= ! swap* offset
1042
- @inbounds v[j] = pivot
1043
+ j = if swap
1044
+ partition! (v, lo+ offset, hi+ offset, offset, o, t, rev, v, 0 )
1045
+ else
1046
+ partition! (t, lo, hi, - offset, o, v, rev, v, - offset)
1047
+ end
1043
1048
swap = ! swap
1044
1049
1045
1050
# For QuickerSort(), a.lo === a.hi === missing, so the first two branches get skipped
0 commit comments