Sub topic 12
Three-Way Partitioning for Duplicate Keys
This chapter assumes you know the Lomuto partition scheme and the worst-case analysis from earlier chapters, and that you can follow recursive Python code.
Why Duplicates Hurt Two-Way Partitioning
Lomuto's partition moves an element left only when a[j] < pivot. If every element equals the pivot, that test never succeeds. The pivot then lands at index lo, and the recursion runs on a range only one element shorter. That is the same unbalanced split you get on sorted input, so the running time is O(n²).
Real data often has many repeated keys, such as status codes, ages, or country names. Hoare's scheme splits equal keys more evenly, but it still recurses into ranges that are already all equal. That work is wasted.
The fix is to split the range into three groups: less than the pivot, equal to the pivot, and greater than the pivot. The equal group is already in its final position, so the recursion skips it entirely.
The Invariant
Dijkstra's "Dutch national flag" scheme keeps three indices, lt, i, and gt, so that these regions hold at every step:
| Range | Contents |
|---|---|
| `a[lo..lt-1]` | less than pivot |
| `a[lt..i-1]` | equal to pivot |
| `a[i..gt]` | not yet examined |
| `a[gt+1..hi]` | greater than pivot |
Each step looks at a[i] and shrinks the unexamined region by one:
- If
a[i] < pivot, swap it witha[lt], then increase bothltandi. - If
a[i] > pivot, swap it witha[gt]and decreasegt. Leaveiwhere it is. - If
a[i] == pivot, increasei.
The loop ends when i > gt and nothing is left unexamined.
Implementation
def quicksort3(a, lo, hi):
if lo >= hi:
return
pivot = a[lo]
lt, i, gt = lo, lo + 1, hi
while i <= gt:
if a[i] < pivot:
a[lt], a[i] = a[i], a[lt]
lt += 1
i += 1
elif a[i] > pivot:
a[i], a[gt] = a[gt], a[i]
gt -= 1
else:
i += 1
quicksort3(a, lo, lt - 1)
quicksort3(a, gt + 1, hi)
data = [3, 1, 3, 2, 3, 3, 4, 3]
quicksort3(data, 0, len(data) - 1)
print(data) # [1, 2, 3, 3, 3, 3, 3, 4]The two recursive calls skip lt..gt, which is the block of keys equal to the pivot.
Common Errors
- Increasing
iafter thegtswap. The element that comes in froma[gt]has not been examined yet. If you skip it, a large value can stay in the middle of the array. - Recursing on
lo..ltorgt..hi. Those ranges include one pivot-equal element. The result is still correct, but you lose part of the benefit. - Using a fixed first-element pivot on sorted input. Three-way partitioning fixes the problem with duplicates. It does not fix bad pivot choice. Combine it with a random pivot by swapping a random element into
a[lo]first.
Exercises
Exercise
Trace quicksort3 on [2, 2, 2, 2]. How many comparisons against the pivot does it make, and what are the recursive calls?
Show answer
It starts with lt=0, i=1, gt=3. All three elements from index 1 to 3 equal the pivot, so i moves forward three times and the loop makes 3 comparisons. It then calls quicksort3(a, 0, -1) and quicksort3(a, 4, 3), and both return right away. The whole array is sorted in one linear pass.
Exercise
Why does Lomuto quicksort take O(n²) time on an array of n equal keys, and which line of quicksort3 avoids that cost?
Show answer
Lomuto's test a[j] < pivot never succeeds, so every partition returns p = lo and leaves a subproblem of size n-1. In quicksort3, the else branch (i += 1) puts equal keys into the middle region, and the recursion never visits that region.