Sub topic 3
Median-of-Three Pivot Selection
This chapter assumes you already know Lomuto partitioning, recursive quicksort, and why a bad pivot choice causes O(n^2) time.
Median-of-Three Pivot Selection
Median-of-three looks at the first, middle, and last elements of a range and uses their median as the pivot. On sorted or reverse-sorted input this picks the true middle, so the splits stay balanced. It costs only a few comparisons per partition. It is not a guarantee: crafted "median-of-three killer" inputs can still force quadratic time.
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
def median_of_three_partition(a, lo, hi):
mid = (lo + hi) // 2
trio = sorted([(a[lo], lo), (a[mid], mid), (a[hi], hi)])
m = trio[1][1]
a[m], a[hi] = a[hi], a[m]
return lomuto_partition(a, lo, hi)
def quicksort_bounded(a, lo, hi):
while lo < hi:
p = median_of_three_partition(a, lo, hi)
if p - lo < hi - p:
quicksort_bounded(a, lo, p - 1)
lo = p + 1
else:
quicksort_bounded(a, p + 1, hi)
hi = p - 1
data = list(range(10))
quicksort_bounded(data, 0, len(data) - 1)
print(data) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]The Hoare Partition Scheme
Hoare's scheme moves two indices toward each other and swaps any pair that is on the wrong side. It does fewer swaps than Lomuto. The returned index p is only a split point, so the recursion covers lo..p and p+1..hi.
Three-Way Partitioning for Duplicate Keys
When many keys are equal, two-way schemes keep re-partitioning runs of equal values. Dijkstra's three-way partition splits the range into less-than, equal, and greater-than regions and recurses only on the outer two. An array of all-equal keys then sorts in O(n).
Bounding Recursion Depth
quicksort_bounded above recurses on the smaller side and loops over the larger one. Each recursive call handles at most half the range, so the stack depth stays at O(log n) even when the splits are bad.
Switching to Insertion Sort for Small Subarrays
For tiny ranges (often 10 to 20 elements), insertion sort is faster than quicksort because its overhead is lower. Libraries stop recursing below a cutoff and finish those ranges with insertion sort.
Introsort: Quicksort in Production Libraries
Introsort runs quicksort but tracks recursion depth. If the depth passes about 2·log2(n), it switches to heapsort, which guarantees O(n log n). C++ std::sort uses this design.
| Technique | Problem it fixes |
|---|---|
| Median-of-three | Sorted input causing bad splits |
| Three-way partition | Many duplicate keys |
| Smaller-side recursion | Stack overflow |
| Insertion-sort cutoff | Overhead on tiny ranges |
| Heapsort fallback | Adversarial worst case |
Quickselect: Finding the k-th Smallest Element
Quickselect partitions the same way but continues into only the side that holds index k. With random pivots its expected time is O(n), because it never sorts the other side.
Exercise
Why does Hoare's scheme recurse on lo..p instead of lo..p-1?
Show answer
Hoare's partition does not place the pivot at its final index p. It only guarantees that elements in lo..p are at most the pivot and elements in p+1..hi are at least the pivot. That means a[p] may still need sorting.