Sub topic 3
The Lomuto Partition Scheme
This chapter assumes you can read Python and follow index-based loops over arrays. It also assumes you know what partitioning around a pivot means: elements smaller than the pivot go to its left, and the rest go to its right. Here you will implement one concrete way to do that, the Lomuto partition scheme, and learn the invariant that makes it correct.
The Working Model
Lomuto partitioning works on the subarray a[lo..hi] (both ends inclusive) and uses the last element, a[hi], as the pivot. It scans left to right with an index j and keeps a second index i, which marks the boundary of the "less than pivot" region. When a[j] is smaller than the pivot, the function swaps it into position i and moves the boundary forward by one. When the scan finishes, it swaps the pivot into position i, which is the pivot's final sorted position, and returns i.
def lomuto_partition(a, lo, hi):
pivot = a[hi]
i = lo
for j in range(lo, hi):
if a[j] < pivot:
a[i], a[j] = a[j], a[i]
i += 1
a[i], a[hi] = a[hi], a[i]
return i
data = [5, 3, 8, 1, 9, 2]
p = lomuto_partition(data, 0, len(data) - 1)
print(p, data) # 1 [1, 2, 8, 5, 9, 3]The Loop Invariant
At the start of each loop iteration, the subarray is split into four regions:
| Region | Contents |
|---|---|
| `a[lo..i-1]` | elements less than the pivot |
| `a[i..j-1]` | elements greater than or equal to the pivot |
| `a[j..hi-1]` | elements not yet examined |
| `a[hi]` | the pivot |
When a[j] is less than the pivot, swapping it with a[i] moves it to the end of the "less" region. The element it displaces was the first element of the "≥" region, so that element simply moves to the back of the same region. When the loop ends, the unexamined region is empty. The final swap places the pivot between the two remaining regions.
Tracing an Example
Take [5, 3, 8, 1, 9, 2] with pivot 2:
j=0..2: the values 5, 3, and 8 are all at least 2, so nothing moves andistays 0.j=3: 1 is less than 2, so the function swapsa[0]anda[3], giving[1, 3, 8, 5, 9, 2]. Nowi = 1.j=4: 9 is at least 2, so nothing moves.- Final swap of
a[1]anda[5]gives[1, 2, 8, 5, 9, 3]. The function returns 1.
The pivot 2 is now at index 1, where it belongs in sorted order. The two sides are not sorted yet. That is the job of the recursive calls.
Common Errors
- Forgetting the final swap. Without it, the pivot stays at
hi, and the returned index does not hold the pivot. - Off-by-one in the loop bounds.
range(lo, hi)excludes the pivot on purpose. Writingrange(lo, len(a))ignoreshiand scans outside the subarray. - Mixing up variants. Some textbooks start with
i = lo - 1and incrementibefore each swap, then returni + 1. Both versions are correct, but combining half of one with half of the other is not. - Expecting good behavior on duplicates. If every element equals the pivot,
a[j] < pivotis never true. The pivot ends up athi, and one side of the split is empty. A later chapter fixes this with three-way partitioning.
Exercise
What does lomuto_partition([4, 7, 1, 6, 3], 0, 4) return, and what is the array afterward?
Show answer
The pivot is 3. Only 1 (at j=2) is smaller than 3, so a[0] and a[2] are swapped, giving [1, 7, 4, 6, 3], with i=1. The final swap of a[1] and a[4] gives [1, 3, 4, 6, 7]. The function returns 1.
Exercise
If you change the comparison to a[j] <= pivot, is the partition still correct? What changes?
Show answer
Yes, it is still correct. Elements equal to the pivot now go to the left region instead of the right, so the invariant becomes "left side is ≤ pivot." On an array where every key is equal, the pivot ends up at hi instead of lo, so one side of the split is still empty.