Quick Sort
Quick Sort
4. Base case: When sublists are of length 0 or 1, they are already sorted.
Example
Sort the list [7, 2, 1, 8, 6]:
• Pick pivot = 7
• Partition:
– Left: [2, 1, 6]
– Pivot: [7]
– Right: [8]
• Recursively sort [2, 1, 6] and [8]
• Final result: [1, 2, 6, 7, 8]
1
Advantages
• Very fast in practice.
• Requires little extra memory.