Quick Sort: Lomuto vs Hoare Partitioning & Pivot Strategies
Quick Sort: Lomuto vs Hoare Partitioning & Pivot Strategies
Quick Sort is an in-place divide-and-conquer sorting algorithm. In practical benchmarks, Quick Sort is typically 2x to 3x faster than Merge Sort and Heap Sort due to excellent CPU hardware cache locality and minimal memory movement.
1. Real-World Analogy: Sorting Students by Height Around a Reference
Imagine a gym teacher organizing 30 students by height:
- The teacher picks one student (say, Alex) to stand in the center as the Pivot.
- The teacher instructs: "Anyone shorter than Alex, move to his left. Anyone taller than Alex, move to his right."
- Notice: Alex is now standing at his exact final sorted position forever!
- The teacher repeats the exact same instruction recursively for the group on the left and the group on the right.
2. The Two Famous Partitioning Schemes
1. Lomuto Partition Scheme (Simpler to Understand)
- The Pivot is chosen as the last element
arr[high]. - A slow pointer
imaintains the boundary of elements smaller than the pivot. - A fast pointer
jscans fromlowtohigh - 1. - When
arr[j] <= pivot, incrementiand swaparr[i]witharr[j].
2. Hoare Partition Scheme (Original & More Efficient)
- Two pointers start at the outer ends (
low - 1andhigh + 1) and advance inward toward each other. - Makes approximately three times fewer swaps on average than Lomuto!
3. The Pivot Selection Trap ($O(n^2)$ Worst-Case)
If you always choose the first or last element as the pivot:
- If the input array is already sorted or reverse sorted, the pivot produces a degenerate partition of size $0$ and $n - 1$.
- The recursion tree degenerates from a balanced tree ($\log n$) into a linked list ($n$), resulting in worst-case $O(n^2)$ quadratic time!
The Remedy: Randomized Pivot or Median-of-Three
Pick three candidates (first, middle, last), find their mathematical median, and swap it to the pivot position. This guarantees balanced $O(n \log n)$ partitions in production!
4. Multi-Language Implementations: Quick Sort (Lomuto Partition)
Python 3:
Modern JavaScript / TypeScript:
5. Trace Table: Partitioning [10, 80, 30, 90, 40, 50, 70] (Pivot = 70)
j | arr[j] | arr[j] <= 70? | i | Swap Action | Array State |
|---|---|---|---|---|---|
| Init | - | - | -1 | None | [10, 80, 30, 90, 40, 50, 70] |
| 0 | 10 | Yes | 0 | Swap arr[0] with arr[0] | [10, 80, 30, 90, 40, 50, 70] |
| 1 | 80 | No | 0 | None | [10, 80, 30, 90, 40, 50, 70] |
| 2 | 30 | Yes | 1 | Swap arr[1] (80) with arr[2] (30) | [10, 30, 80, 90, 40, 50, 70] |
| 3 | 90 | No | 1 | None | [10, 30, 80, 90, 40, 50, 70] |
| 4 | 40 | Yes | 2 | Swap arr[2] (80) with arr[4] (40) | [10, 30, 40, 90, 80, 50, 70] |
| 5 | 50 | Yes | 3 | Swap arr[3] (90) with arr[5] (50) | [10, 30, 40, 50, 80, 90, 70] |
| Final | - | - | - | Swap arr[4] (80) with pivot 70 | [10, 30, 40, 50, 70, 90, 80] |
Pivot 70 is placed at index 4! All items to its left are $< 70$, all to its right are $> 70$.
Multiple Choice Questions
Binary Tree Traversals: DFS (Pre, In, Post) & BFS Level-Order
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.