I've heard that in quicksort it is better to call recursion on the smaller subarray first. For example if 5
was the pivot and the data gets sorted to 4,1,3,5,7,6
then it's better to sort the subarray 7,6
first because it contains two elements where as 4,1,3
contains three.
The source of all knowledge gives the pseudocode for quicksort
quicksort(A, i, k):
if i < k:
p := partition(A, i, k)
quicksort(A, i, p - 1)
quicksort(A, p + 1, k)
So an algorithm that would implement away to recurse on the smaller array first would look something like
quicksort(A, i, k):
if i < k:
p := partition(A, i, k)
if(p-i > k-p)
/*difference from start point to pivot is greater than
difference from pivot to end point*/
quicksort(A, p + 1, k)
quicksort(A, i, p - 1)
else
quicksort(A, i, p - 1)
quicksort(A, p + 1, k)
I profiled code like this written in Java and it does appear to be faster but why? At first I thought it had to do with tail recursion optimization but then realized that was definilty wrong as Java doesn't support it.
Why is sorting the code in the smaller subarray first faster? This article claims it should be