Sub topic 6
Deriving O(n log n) from the Recurrence
This chapter assumes you already know the top-down merge sort from the earlier chapters, Big-O notation, and logarithm basics such as log₂ 8 = 3. Here we turn the shape of the algorithm into a recurrence and solve it.
Writing the Recurrence
Let T(n) be the time merge sort takes on n elements. Each call does three things:
- It splits the list at the midpoint. Python's slicing copies elements, so the split costs time proportional to n.
- It makes two recursive calls, each on about n/2 elements.
- It merges the two sorted halves. Every element is written exactly once, which costs time proportional to n.
The split and the merge together cost at most cn for some constant c. A list of length 0 or 1 returns right away. This gives:
- T(1) = c
- T(n) = 2T(n/2) + cn
To keep the arithmetic exact, assume n is a power of two. We will remove that assumption at the end.
Solving It with a Recursion Tree
Draw each call as a node and label it with the non-recursive work it does. The root does cn of work. Its two children each do c(n/2) of work, and their four children each do c(n/4).
Level k of the tree has 2ᵏ nodes, and each node handles n/2ᵏ elements. The work on that level is:
2ᵏ · c(n/2ᵏ) = cn
The halving stops when n/2ᵏ = 1, which happens at k = log₂ n. That gives log₂ n + 1 levels, and each level costs cn:
T(n) = cn(log₂ n + 1) = cn log₂ n + cn = O(n log n)
Checking by Substitution
You can confirm the closed form by plugging it back in. Guess T(n) = cn log₂ n + cn:
2T(n/2) + cn = 2[c(n/2) log₂(n/2) + c(n/2)] + cn = cn(log₂ n − 1) + cn + cn = cn log₂ n + cn
The guess satisfies the recurrence, and it also gives T(1) = c. This matches case 2 of the Master Theorem: a = 2, b = 2, and f(n) = Θ(n) = Θ(n^(log_b a)), so T(n) = Θ(n log n).
Measuring It
The program below counts how many element writes the merges make. With n a power of two, the count equals n log₂ n exactly.
import math
def merge_sort_count(items):
if len(items) <= 1:
return list(items), 0
mid = len(items) // 2
left, left_writes = merge_sort_count(items[:mid])
right, right_writes = merge_sort_count(items[mid:])
result = []
i = j = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result.extend(left[i:])
result.extend(right[j:])
return result, left_writes + right_writes + len(result)
for n in [8, 64, 1024]:
_, writes = merge_sort_count(list(range(n, 0, -1)))
print(n, writes, n * int(math.log2(n)))
# 8 24 24
# 64 384 384
# 1024 10240 10240When n Is Not a Power of Two
For general n, the halves have sizes ⌈n/2⌉ and ⌊n/2⌋. Each level of the tree still does at most cn work, and the depth becomes ⌈log₂ n⌉. The bound stays Θ(n log n). A common mistake is to count only the n leaves and conclude the sort is O(n). The leaves are cheap, but there are log n levels of merging above them, and each of those levels costs n.
Exercise
Suppose a variant splits the list into three parts of size n/3 and merges them in linear time, so T(n) = 3T(n/3) + cn. What is its running time, and is it asymptotically faster than ordinary merge sort?
Show answer
Each level of the recursion tree still costs cn, and there are log₃ n + 1 levels, so T(n) = Θ(n log₃ n). Because log₃ n = log₂ n / log₂ 3, this differs from n log₂ n only by a constant factor. Both are Θ(n log n), so the variant is not asymptotically faster.
Exercise
If the non-recursive work per call were constant instead of linear, giving T(n) = 2T(n/2) + c, what would T(n) be?
Show answer
Level k now costs 2ᵏ · c. Summing over the levels gives c(1 + 2 + 4 + ... + n) = c(2n − 1), so T(n) = Θ(n). The linear-time merge is what adds the log n factor.