B2.4.4–5 · HL recursion

Quicksort · HL application

Python · step by step

Quicksort · HL application

Partition around a pivot, sort the smaller problems, then combine their results.

Select Play or Next line to follow Python execution.

Python implementation

The highlight marks the next line to execute. Values show the state before that line.

def quicksort(items):    if len(items) <= 1:        return items    pivot = items[0]    smaller = []    greater = []    for value in items[1:]:        if value < pivot:            smaller.append(value)        else:            greater.append(value)    left = quicksort(smaller)    right = quicksort(greater)    return left + [pivot] + right

B2.4.4 application: understand how recursion divides the problem. This branching implementation is an illustration, not a B2.4.5 construction/tracing requirement. It creates new lists and is not in-place. Balanced partitions give O(n log n) time; poor pivots can give O(n²).

Keep exploring

Return to the recursion lesson →

All B2.4 animations →

All practice & resources →