Sub topic 2
Partitioning an Array Around a Pivot
This chapter assumes you can write Python functions, index into lists, and read Big-O notation. It defines the partition step that every version of quicksort relies on. The next chapter turns this definition into a working in-place algorithm.
What partitioning means
A pivot is one element chosen from the range being sorted. To partition the range a[lo..hi] around that pivot, you rearrange its elements so that:
- The pivot sits at some index
p. - Every element in
a[lo..p-1]is less than or equal to the pivot. - Every element in
a[p+1..hi]is greater than or equal to the pivot.
The function returns p. Neither side has to be sorted. They only need to fall on the correct side of the pivot.
Why the pivot's index is final
Say k elements are smaller than the pivot. In the fully sorted array, the pivot then belongs at position lo + k. Partitioning places it there. Later steps sort the left side and the right side separately, and neither one ever moves an element across p. The pivot is therefore never touched again. Each partition call permanently places one element and leaves two smaller, independent problems.
The contract in code
The version below is simple and uses extra space. Its purpose is to make the contract precise before you see the in-place schemes. It writes the partitioned result back into a[lo..hi] and returns p. The checker tests the three conditions listed above.
def partition_copy(a, lo, hi, pivot_index):
pivot = a[pivot_index]
rest = [a[i] for i in range(lo, hi + 1) if i != pivot_index]
left = [x for x in rest if x < pivot]
right = [x for x in rest if x >= pivot]
a[lo:hi + 1] = left + [pivot] + right
return lo + len(left)
def is_partitioned(a, lo, hi, p):
pivot = a[p]
return (all(a[i] <= pivot for i in range(lo, p)) and
all(a[i] >= pivot for i in range(p + 1, hi + 1)))
data = [5, 3, 8, 1, 9, 2]
p = partition_copy(data, 0, len(data) - 1, 5)
print(data, p) # [1, 2, 5, 3, 8, 9] 1
print(is_partitioned(data, 0, len(data) - 1, p)) # TrueKeep is_partitioned around. When you write the in-place versions in later chapters, run it on random inputs to catch off-by-one bugs.
Common errors
| Mistake | Symptom |
|---|---|
| Returning the pivot's value instead of its index | The recursion uses the wrong bounds |
| Leaving the pivot inside one of the sides | The recursion never shrinks, which can cause infinite recursion |
| Using `<` on one side and `>` on the other, so equal keys get dropped | Elements disappear from the output |
| Expecting the sides to come out sorted | Confusion when debugging. Only the pivot's position is guaranteed |
What partitioning costs
A partition looks at each of the hi - lo + 1 elements a constant number of times, so it runs in linear time. The total cost of quicksort depends on how evenly the pivot splits the range, and the analysis chapter covers that in detail.
Exercise
After partitioning [7, 4, 6, 1, 6, 3] with pivot 4, what index p must the function return, assuming lo = 0? Explain why the answer does not depend on how the algorithm is implemented.
Show answer
p = 2. Two elements (1 and 3) are less than 4, so 4 belongs at index 0 + 2. Any correct partition must put all smaller elements before the pivot and none of them after it. That fixes the pivot's position no matter which scheme you use.
Exercise
The checker is_partitioned returns True for the array [3, 1, 2] with p = 2. Is it a valid partition, and is the subarray sorted?
Show answer
It is a valid partition, because 3 and 1 are both less than or equal to the pivot 2. That claim is false for 3, though: 3 is greater than 2, so the checker actually returns False and the arrangement is invalid. The point is that the checker tests each element against the pivot. It does not test whether a side is sorted.