Sub topic 3
Splitting Until One Element Remains
This chapter assumes you are comfortable with Python lists and slicing, recursion and the call stack, and basic Big-O notation. It covers only the "divide" half of merge sort: how a list is cut into smaller pieces, and when the cutting stops. Combining the pieces again is the job of the merge step, which has its own chapter.
The splitting rule
Merge sort splits a list of length n at the index mid = n // 2. The left half is items[:mid] and the right half is items[mid:]. When n is odd, the right half gets the extra element. For example, a list of 7 elements splits into halves of 3 and 4.
Each half is split again by the same rule. The splitting continues until every piece is too small to need sorting.
The base case
A list with zero or one element is already sorted. This is the base case, the input the recursion handles directly without making another call. In code, the test is len(items) <= 1.
The base case must include the empty list. A caller will rarely pass [], but a sort function should handle it. If you test only len(items) == 1, an empty list never stops. With n = 0, mid is 0, and both halves are [] again, so the function calls itself forever.
Watching the splits happen
The function below performs only the splitting and prints each piece, indented by its recursion depth:
def split_trace(items, depth=0):
print(" " * depth + str(items))
if len(items) <= 1:
return
mid = len(items) // 2
split_trace(items[:mid], depth + 1)
split_trace(items[mid:], depth + 1)
split_trace([38, 27, 43, 3, 9, 82, 10])Output:
[38, 27, 43, 3, 9, 82, 10]
[38, 27, 43]
[38]
[27, 43]
[27]
[43]
[3, 9, 82, 10]
[3, 9]
[3]
[9]
[82, 10]
[82]
[10]The calls run depth-first. The whole left branch finishes before the right branch starts. This ordering matters later, because the merges happen as each call returns.
Why the recursion always ends
For any n >= 2, mid = n // 2 satisfies 1 <= mid < n. Both halves are therefore non-empty and strictly shorter than the input. Every call works on a shorter list, so every path through the tree eventually reaches length 1.
Each level roughly halves the size, so the tree has about ⌈log₂ n⌉ levels. A million elements need only about 20 levels. Python's default recursion limit of 1000 is never a concern here.
Common errors
| Mistake | Effect |
|---|---|
| Base case `len(items) == 1` | An empty input recurses forever |
| Left half `items[:mid + 1]` | For `n = 2`, the left half is the whole list, so the recursion never ends |
| Base case `len(items) <= 2` without sorting the pair | Two-element pieces are returned unsorted |
| Splitting off one element at a time (`items[:1]`, `items[1:]`) | Still correct, but the depth becomes `n` and the running time degrades to O(n²) |
Exercises
Exercise
How many times is split_trace called for a list of 7 elements, and how many of those calls hit the base case?
Show answer
It is called 13 times. Each of the 7 elements ends up in its own base-case call, and there are 6 internal calls that split. A binary tree with 7 leaves where every internal node has two children has 7 − 1 = 6 internal nodes.
Exercise
A student writes mid = (len(items) + 1) // 2 and keeps the base case len(items) <= 1. Does the recursion still terminate? Why?
Show answer
Yes. For n >= 2, (n + 1) // 2 is at least 1 and at most n − 1. Both halves are non-empty and shorter than the input. The only change is that the left half gets the extra element when n is odd.