Sub topic 4
Putting It Together: Recursive Quicksort
This chapter assumes you can read Python, follow recursive calls on the call stack, and already know how the Lomuto partition from the previous chapter places a pivot at its final index. The task here is to combine that partition step with recursion to get a complete in-place sort.
The Recursive Structure
Quicksort on a range a[lo..hi] follows three rules:
- If the range has fewer than two elements, it is already sorted, so stop.
- Partition the range. The pivot lands at index
p. Everything left ofpis smaller, and everything right ofpis greater or equal. - Sort
a[lo..p-1]anda[p+1..hi]recursively.
No merge step is needed. Once both sides are sorted, the whole range is sorted, because the partition already placed every element on the correct side of the pivot.
The Complete Implementation
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 quicksort(a, lo=0, hi=None):
if hi is None:
hi = len(a) - 1
if lo < hi:
p = lomuto_partition(a, lo, hi)
quicksort(a, lo, p - 1)
quicksort(a, p + 1, hi)
data = [5, 3, 8, 1, 9, 2]
quicksort(data)
print(data) # [1, 2, 3, 5, 8, 9]The hi=None default lets callers write quicksort(data) without passing bounds. The function sorts the list it is given and returns None.
Tracing a Run
Here is what happens on [5, 3, 8, 1, 9, 2]:
| Call range | Pivot | Array after partition | p |
|---|---|---|---|
| 0..5 | 2 | [1, 2, 8, 5, 9, 3] | 1 |
| 0..0 | none | base case | none |
| 2..5 | 3 | [1, 2, 3, 5, 9, 8] | 2 |
| 2..1 | none | empty, base case | none |
| 3..5 | 8 | [1, 2, 3, 5, 8, 9] | 4 |
| 3..3, 5..5 | none | base cases | none |
The tree is lopsided: pivot 3 was the smallest value in its range, so one side came out empty. The shape of this tree determines the running time, which the next chapter analyzes.
Common Errors
- Including the pivot in a recursive call. Writing
quicksort(a, lo, p)looks harmless, but Lomuto's pivot is already in its final place. If the pivot is the largest value,p == hi, and the call recurses on the same range forever until Python raisesRecursionError. - Testing the default with
if not hi. This treatshi == 0as missing and resets it tolen(a) - 1, which breaks the recursion. Testhi is None. - Using
lo <= hias the recursion condition. It is still correct, but it adds a useless call for every one-element range. - Expecting a return value.
sorted_data = quicksort(data)givesNone, because the function changesdatain place.
Exercise
Change the recursive calls to quicksort(a, lo, p) and quicksort(a, p + 1, hi), then run quicksort([1, 2, 3]). What happens, and why?
Show answer
The first partition uses pivot 3, which is the largest value, so p = 2 = hi. The call quicksort(a, 0, 2) then repeats on the same range with the same pivot and never shrinks, and Python raises RecursionError. Lomuto places the pivot at its final index, so the recursive calls must exclude p.
Exercise
How many calls to lomuto_partition does quicksort([5, 3, 8, 1, 9, 2]) make?
Show answer
Three: on ranges 0..5, 2..5, and 3..5. Every other call hits the base case lo >= hi and returns without partitioning.