When a list is too large to sort comfortably by inserting one item at a time, we can try a different idea. Split it into two smaller lists, sort each one, then merge the sorted results. The smaller lists can be split again. Eventually every piece has one item, which is already sorted.
Background for this page. A recursive function calls itself on smaller problems. A base case returns without another call. You should be comfortable following a loop and a function call. Related explanations: Insertion sort: a worked example and its cost · Big O, Big Omega, and Big Theta.
That pattern is called divide and conquer. It has three parts: divide the problem into smaller versions of itself, solve those smaller versions, and combine their answers. The last part is easy to overlook. Splitting helps only when we know how to put the pieces back together at a reasonable cost.
4.1 Follow the pieces
Start with [7, 3, 5, 2]. Split it into [7, 3] and [5, 2], then split each pair into single values. Merge [7] with [3] to get [3, 7]. Merge [5] with [2] to get [2, 5]. Finally, merge those two sorted pairs to get [2, 3, 5, 7].
The diagram shows a question we should ask at every merge: which first item is smaller? If the left side starts with 3 and the right side starts with 2, take 2. Then compare 3 with 5, and keep going. Each item moves into the merged result once. That makes the merge work grow with the number of items being merged.
There is also a base case: a list of zero or one item needs no splitting. Without a base case, a recursive method has no reason to stop. A recursive call must also get a smaller problem; otherwise it may never reach the base case.
4.2 Write the method
Here is one Python version. It returns a new sorted list and leaves the input alone.
# Return a new list with the values in order.
def merge_sort(values):
# A list this short is already sorted.
if len(values) <= 1:
return list(values)
# Sort each half by the same method.
middle = len(values) // 2
left = merge_sort(values[:middle])
right = merge_sort(values[middle:])
# Merge the two sorted halves.
result = []
i = j = 0
while i < len(left) and j < len(right):
# Take the smaller front item.
if left[i] <= right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
# One half may still have items left.
result.extend(left[i:])
result.extend(right[j:])
return result
There are two reasons this works. The recursive calls return sorted halves. Then the merge always takes the smallest item still available at the front of either half. As long as both halves were sorted, no smaller item is hidden behind a larger one. When one half runs out, we can append the rest of the other half in its existing order.
The <= matters for equal values. When the front items are equal, this code takes the one from the left half first, so equal items keep their earlier relative order. This is a stable sort. Python list slices and the merged result use extra memory; this version is written for clarity rather than for sorting in place.
4.3 Describe the work without timing it
At each level of splitting, merging all the pieces takes work proportional to n, the number of items. If each split roughly halves a list, the number of levels grows like log₂ n. Multiply the work per level by the number of levels and you get Θ(n log n) work for merge sort. The result is a growth claim, not a promise about how many milliseconds a particular computer will take.
We can write the same reasoning as a recurrence. If T(n) is the work for n items, then, ignoring the small difference between the two halves when n is odd,
T(n) = 2T(n/2) + Θ(n).
The two smaller calls are the 2T(n/2) part. Merging is the Θ(n) part. The base case says T(1) is bounded by a constant. A recursion tree draws the work at each level and makes the n log n result visible. A substitution proof checks a proposed bound by putting it back into the recurrence. The master method handles a useful family of recurrences of this form. We will use those tools when they help; the main habit is to identify the smaller calls and the work needed to combine their answers.
4.4 A harder example: multiplying matrices
Divide and conquer also applies when the pieces are less obvious. A square matrix can be divided into four blocks. To multiply two such matrices by the straightforward block method, we calculate eight smaller matrix products and add them in the right places. The recurrence has eight half-size products plus work to combine them, giving cubic growth in the usual analysis.
Strassen’s method uses seven smaller products instead of eight, with more additions and subtractions. Its recurrence is T(n) = 7T(n/2) + Θ(n²). Solving that recurrence gives Θ(nlog₂ 7), or about n2.81. That is a striking result, though the extra work can make it a poor choice for small matrices. The point is that changing the number of smaller problems can change the overall cost, even when the input is split in exactly the same way.
The book develops several ways to solve the recurrences behind these algorithms, including the substitution method, recursion trees, and the master method. More advanced recurrence results belong to deeper study. For now, carry the simple question forward: How many smaller problems do we create, how small are they, and what does combining them cost?
Why merge sort works
Merge [1, 8] and [3, 6]. Take 1, then 3, then 6, and finally 8. Each selected front item is the smallest remaining item across both lists. This proves the merge step produces sorted output without losing values. Lists of length zero or one are already sorted. If recursive calls correctly sort smaller halves, merging them correctly sorts the whole list. These two statements give an induction proof. The Python example allocates slices and a result list; it returns a new list rather than sorting in place.
Practice. Split [8, 1, 6, 3] into single items, then show the sorted pairs and final merge. What is the base case? Why does splitting without merging fail to sort the original list?
Answer. The single items are [8], [1], [6], and [3]. They become [1, 8] and [3, 6], then [1, 3, 6, 8]. A zero- or one-item list is the base case. Splitting produces small sorted pieces, but the original list is sorted only after those pieces are combined in order.
Next we will ask what changes when an algorithm uses a random choice, and how to reason about its cost without expecting the same run every time.
Try another case. Must the two halves have equal lengths?
Reasoning. No. Splitting seven items into groups of three and four works. Each group must be smaller, and the merge must preserve all items in order.