Quicksort - the Easy Way

Note : For 3-way partition and Dual Pivot Quicksort (with programs to trace the sort process), please refer to this recent post.

Quick sort is the fastest known non-hybrid comparision sort for arrays which has no knowledge of the data beforehand. To top it, it could be done in-place for arrays. For Linked Lists, Merge Sort might be a better option. Also since Quicksort improves its performance by using random numbers, it also becomes an example of a breed of algorithms named “Randomized Algorithms”.

The naive version of the Algorithm goes like this :

1)  Start with list I of n items
2)  Choose pivot v from I
3)  Partition I into 2 unsorted lists I1 and I2 such that
        I1 : all keys smaller than pivot
        I2 : all keys larger than pivot
        Items same as pivot goes to either list
4)  Sort I1 recursively, yielding sorted list S1
5)  Sort I2 recursively, yielding sorted list S2
6)  Concatenate S1,v,S2 yielding sorted list S

Pseudocode in a Video

How Long does Quick sort take ?

Interestingly, Quicksort has a worst case running time of 0(n2). However, with careful implementations, n2 never happens and it almost always runs in 0(n log n). As compared to Heapsort, whose best and worst case is guaranteed to be n log n, Quicksort beats Heapsort in clock time for two reasons :

1) The constant factor surrounding the 0(nlogn) is lesser than Heapsort due to the very thin core logic. Heap sort’s detailed comparison and exchange mechanism inside loops increases the constant factor.

2) Quicksort could leverage on the locality of reference (and therefore, memory access) advantages provided by the hardware and the operating system

How n log n?

If you choose the pivot right, then, on an average, you get a 1/4, 3/4 split which gives an average running time of 0(n log n)

Source : Goodrich and Tamassia

Pivot Choosing strategy :

There are many methods to choose a pivot

1)  First Item
2)  Last item 
3)  Median
4)  Median of k (generally of 3)
5)  Random item 

Choosing a random key as the pivot is the preferred method because of the guaranteed worst case possibility with the other selection methods with following kinds of sequences :

1)  Presorted list 
        when chosing first item as your pivot in case of natural order
                     last item as your pivot in case of reverse order
2)  Repeated elements   
3)  Unimodal sequences* (when median is chosen as pivot)

Bottom line : When you choose a pivot in such a way that one portion of your split (either I1 or I2) becomes empty AND if you are doing it all through the list, then you are essentially doing the split N times.

Quicksort in Java


Goodrich and Tamassia
Three Beautiful Quicksorts
Quicksort is Optimal
Unimodal sequences