Saturday, July 24, 2010

QUICK SORT TECHNIQUE WITH LIST OF ELEMENTS

Full example of quicksort on a random set of numbers. The boxed element is the pivot. It is always chosen as the last element of the partition. However, always choosing the last element in the partition as the pivot in this way results in poor performance (O(n2)) on already sorted lists, or lists of identical elements. Since sub-lists of sorted / identical elements crop up a lot towards the end of a sorting procedure on a large set, versions of the quicksort algorithm which choose the pivot as the middle element run much more quickly than the algorithm described in this diagram on large sets of numbers.



QUICK SORT TECHNIQUE WITH DIAGRAM


Visualization of the quicksort algorithm. The horizontal lines are pivot values.
ClassSorting algorithm
Worst case performance\mathcal{O}(n^2)
Best case performance\mathcal{O}(n\log n)
Average case performance\mathcal{O}(n\log n)
Worst case space complexity\mathcal{O}(n)