What?
A type of Sorting Algorithms. When you hear it, think Pivot. Criteria of a pivot:
- All items to the left are smaller
- Items to the right are bigger
How does it work?
- Choose a Pivot and move it to the end
- Set
Pointer Red
() andPointer b
() to the first element in the given array. - If Pivot: Swap items pointed to by both and .
- If so, increment
- Increment .
- Stop when is sent out of bounds.
- Recursively repeat this for each sides of the pivot.
Loop Invariant For QuickSort:
- Everything is Pivot
- Everything and is Pivot
How do we choose the Pivot?
Popular Option: Meeting of Three:
Sort the first, middle and last indexes. Choose the middle as the pivot. This makes the assumption that the middle of these items is the median of the array.
Time Complexity:
- Worst Case:
- Average Case: