Quicksort
- Worst case , average with a small constant factor.
- Steps
- Divide:
A[p:r]intoA[p:q-1](low side) andA[q+1:r](high side), so thatall(a <= A[p] for a in A[p:q-1]) and all(A[p] <= a for a in A[q+1:p]) - Conquer: sort the two subarrays
- Combine by doing nothing.
- Divide:
- The key to the algorithm is
partitionprocedure, which always choosesA[r]as the pivot, and classify the other elements. - Choosing a good pivot to divide the array into roughly equal half is the key
- Middle element
- Median of three (first, middle, last)
- Random pivot