Worked Examples: Quicksort

These examples are designed to step through the logical process of applying the algorithmic concepts.

Example 1: Lomuto Trace
❓ Problem: Partition [2, 8, 7, 1, 3, 5, 6, 4] with pivot 4.
💡 Solution: Swaps elements <= 4 to left. Returns index of 4.
Example 2: Worst Case Input
❓ Problem: Input causing $O(n^2)$ for deterministic last-pivot.
💡 Solution: Sorted [1,2,3,4] or Reverse [4,3,2,1].
Example 3: Stability
❓ Problem: Is Quicksort Stable?
💡 Solution: No. Partition swaps non-adjacent elements.
Example 4: Space Complexity
❓ Problem: Stack depth?
💡 Solution: Worst $O(n)$, Average $O(\log n)$.
Example 5: Randomized Probability
❓ Problem: Prob of worst case in Randomized QS?
💡 Solution: Extremely low ($1/n!$).
Example 6: Hoare Partition
❓ Problem: Compare to Lomuto.
💡 Solution: Hoare is generally 3x fewer swaps but harder to implement correctly.