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.
Python implementation
1def quicksort(items):2 if len(items) <= 1:3 return items4 pivot = items[0]5 smaller = []6 greater = []7 for value in items[1:]:8 if value < pivot:9 smaller.append(value)10 else:11 greater.append(value)12 left = quicksort(smaller)13 right = quicksort(greater)14 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 →