Sub topic 1
Divide and Conquer: The Idea Behind Quicksort
Quicksort is one of the most widely used sorting algorithms. Before looking at partitioning details, it helps to see the strategy it is built on, which is divide and conquer.
Prerequisites
This chapter assumes you are comfortable with:
- Basic Python: lists, list comprehensions, and functions
- Recursion and how the call stack unwinds
- Big-O notation at the level of O(n) versus O(n²)
- At least one simple sorting algorithm, such as insertion sort
The Three Steps of Divide and Conquer
A divide-and-conquer algorithm solves a problem in three steps:
- Divide the problem into smaller subproblems of the same kind.
- Conquer each subproblem by solving it recursively. Subproblems that are small enough are solved directly; these are the base cases.
- Combine the subproblem solutions into a solution for the original problem.
Merge sort and quicksort both follow this pattern, but they put the work in different places. Merge sort divides trivially by splitting the array in half and does its real work in the combine step, where it merges the halves. Quicksort does its real work in the divide step. It picks a pivot element and rearranges the others so that smaller elements come before the pivot and larger elements come after it. Once that is done, the combine step needs no work, because the sorted left side, the pivot, and the sorted right side are already in the right order.
| Algorithm | Divide step | Combine step |
|---|---|---|
| Merge sort | Split at the midpoint (cheap) | Merge two sorted halves (O(n)) |
| Quicksort | Partition around a pivot (O(n)) | Concatenate (free when done in place) |
A Simple Version
The version below builds new lists instead of sorting in place. It is not efficient, but it shows the structure clearly.
def quicksort_simple(xs):
if len(xs) <= 1:
return xs
pivot = xs[len(xs) // 2]
less = [x for x in xs if x < pivot]
equal = [x for x in xs if x == pivot]
greater = [x for x in xs if x > pivot]
return quicksort_simple(less) + equal + quicksort_simple(greater)
print(quicksort_simple([5, 3, 8, 1, 9, 2, 5])) # [1, 2, 3, 5, 5, 8, 9]Each part of the code maps to one of the three steps. The less, equal, and greater lists are the divide step. The two recursive calls are the conquer step. The + concatenation is the combine step. The base case is a list of length 0 or 1, which is already sorted.
Common Errors
- Leaving out the base case. Without
len(xs) <= 1, the recursion never stops. - Recursing on a list that still contains the pivot. Each recursive call must work on a strictly smaller input. If the pivot goes into
greater, a list of identical values never gets smaller. - Dropping duplicates. If you test
x < pivotandx > pivotbut leave outequal, every copy of the pivot value disappears from the output.
Exercise
Suppose you delete the equal list and change greater to [x for x in xs if x >= pivot], returning quicksort_simple(less) + quicksort_simple(greater). What happens when you call it on [2, 2]?
Show answer
The pivot is 2, so less is empty and greater is [2, 2], the same list you started with. The function calls itself on identical input forever and raises RecursionError. Each recursive call has to shrink the problem, so the pivot (or every copy of it) must stay out of the recursive calls.
Exercise
In quicksort, which of the three divide-and-conquer steps does most of the work, and how does that differ from merge sort?
Show answer
In quicksort the divide step (partitioning) does most of the work, and combining costs nothing. In merge sort, dividing costs almost nothing and the combine step (merging) does most of the work.