Knowledge Base > Algorithm > Sorting
Overview
Quick Sort is well known as ’the best practical sorting algorithm’. It makes Θ(n ⋅ lgn) comparisons on average, and O(n2) comparisons in the worst case. Typically, Quick Sort performs faster than other O(n ⋅ lgn) sorting algorithms.
Algorithm
Pseudocode
|
|
Quicksort sorts by applying a divide and conquer strategy (of method 3.1.6) to divide a list into two sublists, and performs the following operations:
![A[1...i] A[pivot] A[j-+ 1...n]
◟ ◝◜ ◞ ◟ ◝◜ ◞ ◟ ◝◜ ◞
≤ A [pivot] ≥ A[pivot]](kb121x.png)
Time Complexity
The performance of Quick Sort always depends on the position of selected pivot.
A bad pivot could lead to poor performance, for instance, selecting position 1 as the
pivot produces a recursion tree of height n. Conversely, a good pivot would
operate at best performance of O(n ⋅ lgn) of tree height Θ(lgn), pivot at the
middle of the list, for instance. Therefore, trivial cases, such as sorting a list
of sorted element, or of all duplicated elements, have no direct effect on
performance.
Similar to merge sort, Quick sort can at most compare and swap every elements for
each pass, and lg n of iterations for total. Thus, a O(n ⋅ lg n) of comparisons and
swaps can be expected in the worst case.
This brings up with an idea of Randomized Quick Sort, which is implemented to choose the pivot at random.
Ideally, this approach performs at average-case behaviors, but in practice, choosing
the right pivot dominates the performance.
Space Complexity
Quick sort does not sort in place, meaning that a new memory space must be allocated for the sorted output to be stored in whenever the partition operation is performed. The recursive tree can help us view it; only constant space O(1) at each level of partition multiplies the height of the tree, which is lg n. Thus, a total of O(lg n) extra space is used.
Stability
Quick Sort is not a stable sort, because the order of occurrence of duplicated elements is miss-ordered during the operation of swap.
Source Code