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 :
| |
Pseudocode in a Video
How Long does Quick sort take ?
Interestingly, Quicksort has a worst case running time of 0(n^2). However, with careful implementations, n^2 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 :
- 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.
- 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
| |
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 :
| |
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
| |
References