From d78581c8a3941188ce031cea84d498c8b6bf6e9e Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Sun, 16 Nov 2014 12:02:51 -0500 Subject: [PATCH] fix #8347 --- base/sort.jl | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/base/sort.jl b/base/sort.jl index 1caf05487f7be9..542646b563f6bc 100644 --- a/base/sort.jl +++ b/base/sort.jl @@ -281,8 +281,16 @@ function sort!(v::AbstractVector, lo::Int, hi::Int, a::QuickSortAlg, o::Ordering v[i], v[j] = v[j], v[i] end v[j], v[lo] = v[lo], v[j] - lo < (j-1) && sort!(v, lo, j-1, a, o) - lo = j+1 + if j-lo < hi-j + # recurse on the smaller chunk + # this is necessary to preserve O(log(n)) + # stack space in the worst case (rather than O(n)) + lo < (j-1) && sort!(v, lo, j-1, a, o) + lo = j+1 + else + j+1 < hi && sort!(v, j+1, hi, a, o) + hi = j-1 + end end return v end