Skip to content

Commit 4547cb7

Browse files
committed
fix #8347
1 parent 970d83e commit 4547cb7

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

base/sort.jl

+10-2
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,16 @@ function sort!(v::AbstractVector, lo::Int, hi::Int, a::QuickSortAlg, o::Ordering
281281
v[i], v[j] = v[j], v[i]
282282
end
283283
v[j], v[lo] = v[lo], v[j]
284-
lo < (j-1) && sort!(v, lo, j-1, a, o)
285-
lo = j+1
284+
if j-lo < hi-j
285+
# recurse on the smaller chunk
286+
# this is necessary to preserve O(log(n))
287+
# stack space in the worst case (rather than O(n))
288+
lo < (j-1) && sort!(v, lo, j-1, a, o)
289+
lo = j+1
290+
else
291+
j+1 < hi && sort!(v, j+1, hi, a, o)
292+
hi = j-1
293+
end
286294
end
287295
return v
288296
end

0 commit comments

Comments
 (0)