Sub topic 11
The Hoare Partition Scheme
This chapter assumes you already know the Lomuto partition scheme, the recursive quicksort that uses it, and Python's list indexing. It covers the partition scheme Tony Hoare published with quicksort in 1961. Hoare's version does fewer swaps than Lomuto's, but it returns a different kind of index, and the recursion has to account for that.
How the Two Indices Work
Lomuto moves one index forward and builds the "less than pivot" region behind it. Hoare starts one index at each end and moves them toward each other:
imoves right and stops at an element that is greater than or equal to the pivot.jmoves left and stops at an element that is less than or equal to the pivot.- If
i < j, the two elements are on the wrong sides, so they are swapped. - Once
i >= j, the indices have crossed andjis returned.
When the function returns p, every element in a[lo..p] is less than or equal to the pivot, and every element in a[p+1..hi] is greater than or equal to it.
The Code
def hoare_partition(a, lo, hi):
pivot = a[(lo + hi) // 2]
i = lo - 1
j = hi + 1
while True:
i += 1
while a[i] < pivot:
i += 1
j -= 1
while a[j] > pivot:
j -= 1
if i >= j:
return j
a[i], a[j] = a[j], a[i]
def quicksort_hoare(a, lo, hi):
if lo < hi:
p = hoare_partition(a, lo, hi)
quicksort_hoare(a, lo, p)
quicksort_hoare(a, p + 1, hi)
data = [5, 3, 8, 1, 9, 2]
quicksort_hoare(data, 0, len(data) - 1)
print(data) # [1, 2, 3, 5, 8, 9]The Pivot Does Not Land at p
Here is the first partition of [5, 3, 8, 1, 9, 2], where the pivot is a[2] = 8:
| Step | i | j | Array after the step |
|---|---|---|---|
| i stops at 8, j stops at 2, swap | 2 | 5 | [5, 3, 2, 1, 9, 8] |
| i stops at 9, j stops at 1, indices crossed | 4 | 3 | [5, 3, 2, 1, 9, 8] |
The function returns p = 3. The pivot 8 is at index 5, not index 3. Lomuto guarantees that the pivot sits at its final position p. Hoare guarantees only that the two halves are split correctly.
This difference changes the recursion bounds:
| Scheme | What `p` means | Recursive calls |
|---|---|---|
| Lomuto | The pivot is at its final position `p` | `lo..p-1` and `p+1..hi` |
| Hoare | The split point between the two halves | `lo..p` and `p+1..hi` |
Why Use Hoare's Scheme
- Fewer swaps. Hoare swaps only pairs that are both on the wrong side. On random input it does about a third as many swaps as Lomuto.
- Equal keys stay balanced. Both indices stop on elements equal to the pivot, so an array of all-equal values splits near the middle. Lomuto puts every equal element on one side, and its running time becomes O(n²).
- The trade-off. The boundary logic is easier to get wrong, and the pivot's final position is not returned. Quickselect relies on that position, which is one reason Lomuto is still common in teaching code.
Exercise
The worked example above returns p = 3 for [5, 3, 8, 1, 9, 2]. Why is it wrong to recurse on lo..p-1 and p+1..hi with Hoare's scheme?
Show answer
Hoare's scheme does not put an element at its final position p. With p - 1, index p (here, the value 1 at index 3) is left out of both recursive calls and is never sorted. After the calls, 1 would still be at index 3 instead of index 0.
Exercise
Why does the pivot comparison use strict < and > instead of <= and >=?
Show answer
With strict comparisons, both indices stop on elements equal to the pivot. This keeps the indices inside the array, because the pivot itself stops them. It also makes runs of equal keys split evenly. With <= and >=, i could run past hi, and arrays with many duplicates would split unevenly.