Sub topic 1
The Divide-and-Conquer Idea
Merge sort belongs to a family of algorithms built on one strategy: break a problem into smaller copies of itself, solve those copies, and combine their answers. This chapter covers that strategy on its own terms. Later chapters apply it to sorting.
Prerequisites
This chapter assumes you are comfortable with the following:
- Writing functions, loops, and conditionals in Python or a similar language
- Working with lists and slicing by index
- Recursion, including how the call stack holds each pending call
- Reading Big-O notation such as O(n) and O(log n)
The Three Steps
A divide-and-conquer algorithm has three parts:
- Divide. Split the input into smaller subproblems of the same kind.
- Conquer. Solve each subproblem recursively. When a subproblem is small enough, solve it directly. That small case is called the base case.
- Combine. Join the subproblem solutions into a solution for the original input.
The strategy pays off when the combine step costs less than solving the whole problem directly. Merge sort's divide step is trivial: cut the list in half. Its combine step, the merge, does most of the work, and it runs in linear time.
A Small Example Before Sorting
Finding the largest value in a list shows the same three steps without any sorting logic in the way:
def largest(items):
# Base case: a single element is its own maximum.
if len(items) == 1:
return items[0]
# Divide: split into two halves.
mid = len(items) // 2
# Conquer: solve each half recursively.
left_max = largest(items[:mid])
right_max = largest(items[mid:])
# Combine: the answer is the larger of the two results.
return left_max if left_max >= right_max else right_max
print(largest([38, 27, 43, 3, 9, 82, 10])) # 82A simple loop solves this problem just as well, so divide and conquer gives no speedup here. The example is useful for its shape. Merge sort keeps the same skeleton and swaps the one-line combine step for a merge of two sorted lists.
Where People Go Wrong
These mistakes come up often when people first write divide-and-conquer code:
| Mistake | What happens | Fix |
|---|---|---|
| No base case, or one that is never reached | Infinite recursion and a `RecursionError` | Stop at size 1, or size 0 if empty input is allowed |
| Split that can produce an empty half and a full half | The same problem recurses forever | Use `mid = len(items) // 2` with a base case of `len(items) <= 1` |
| Expensive combine step | The total cost can exceed that of a simple loop | Keep the combine linear in the input size |
| Subproblems that overlap | Repeated work blows up the running time | Keep the halves disjoint, or switch to dynamic programming |
Why Halving Matters
Cutting the input in half at every level means the recursion reaches size 1 after about log₂ n levels. If every level does O(n) total work combining results, the total cost is O(n log n). A later chapter derives this from a recurrence. For now, the point is that balanced splits keep the recursion shallow.
Exercise
In the largest function, what goes wrong if you change the base case to if len(items) == 0: return None and call it on a non-empty list?
Show answer
A one-element list splits into an empty left half and a one-element right half. The right half is the same problem as before, so the recursion never makes progress. It ends in a RecursionError, and the comparison would also fail on None. The base case has to stop at size 1, or the split has to guarantee that both halves shrink.
Exercise
Name the divide, conquer, and combine steps of merge sort in one sentence each.
Show answer
Divide: split the list into two halves at the midpoint. Conquer: sort each half recursively, treating lists of length 0 or 1 as already sorted. Combine: merge the two sorted halves into one sorted list.