A Divide and Conquer Sorting Algorithm that is the fastest in Expected Analysis. Time complexity of:
- Worst case
- Average case
- Best case
Algorithm

- Pick a pivot index at random
- Create a sub-list for all indexes smaller than the pivot index
- Create a sub-list for all indexes bigger than the pivot index
- Create a sublist for the item at the pivot index
- Recursively call this pivot algorithm
- Base case is when the list is size of 1
Pseudocode
quick_sort(A):
if p < r:
g := partition(A,g,r)
quicksort(A,g,g-1)
quicksort(A,g+1,r)
partition(A,p,r):
x := A[r]
i := p-1
for j in p, ..., r-1:
if A[j] <= x:
i := i + 1
exchange A[i] with A[j]
exchange A[i+1] with A[r]
return i+1Correctness Proof
Uses Loop Invariant:
- for
- for
- for