
This lesson is part of the Merge Sort series. The series began with recursion and merging. Here we build the same algorithm from the bottom up, using loops instead of recursive calls.

Follow the animation pass by pass
The list is [7, 2, 6, 3, 1]. Bottom-up merge sort starts with one-item runs, so it does not need recursive calls to create them.
- Width 1: start with [7], [2], [6], [3], [1]. Each one-item run is already sorted.
- Merge neighboring runs: combine [7] with [2] to get [2, 7]. Combine [6] with [3] to get [3, 6]. The final [1] has no partner, so carry it forward.
- Width 2: merge [2, 7] and [3, 6]. Compare their front values in turn to make [2, 3, 6, 7]. Carry [1] forward again.
- Width 4: merge [2, 3, 6, 7] with [1]. Since 1 is smaller than the first value 2, write 1 first, then append 2, 3, 6, and 7. The result is [1, 2, 3, 6, 7].
- Finish: the run covers the whole list. Each pass doubles the run width: 1, 2, 4, then 8.
The odd final run is not dropped; it moves to the next pass and is merged when a neighboring run is available.

Why use bottom-up merge sort?
Recursive merge sort divides the input until each piece has one item. Bottom-up merge sort starts with one-item runs and repeatedly merges neighboring runs. Both methods produce the same ordering and take O(n log n) time. The bottom-up version makes the passes explicit and avoids recursion overhead.
The passes
For the input [7, 2, 6, 3, 1], treat each item as a sorted run:
Start: [7] [2] [6] [3] [1]
Pass 1: [2, 7] [3, 6] [1]
Pass 2: [2, 3, 6, 7] [1]
Pass 3: [1, 2, 3, 6, 7]The run width doubles after each pass: 1, 2, 4, 8, and so on. The last run may be shorter. Carry it forward or merge it with the neighboring run. Either way, every value must appear exactly once in the next pass.
Python implementation
This in-place version uses one temporary array for merged values. It writes each merged range back to the original list.
def bottom_up_merge_sort(values):
n = len(values)
buffer = [None] * n
width = 1
while width < n:
start = 0
while start < n:
mid = min(start + width, n)
end = min(start + 2 * width, n)
left, right, out = start, mid, start
while left < mid and right < end:
if values[left] <= values[right]:
buffer[out] = values[left]
left += 1
else:
buffer[out] = values[right]
right += 1
out += 1
while left < mid:
buffer[out] = values[left]
left += 1
out += 1
while right < end:
buffer[out] = values[right]
right += 1
out += 1
values[start:end] = buffer[start:end]
start = end
width *= 2
return values
print(bottom_up_merge_sort([7, 2, 6, 3, 1]))
# [1, 2, 3, 6, 7]Choosing the left value when two values are equal preserves their original order. This makes the sort stable, provided each merge keeps the original left run before the right run.
Complexity and checks
Each pass processes at most n values, and there are at most ceil(log₂ n) passes. Time is O(n log n), and this implementation uses O(n) auxiliary space. Test empty input, one value, odd lengths, duplicate keys, and already sorted input. These cases catch common errors around range boundaries and unfinished runs.
Bottom-up merge sort is useful when an iterative structure is simpler to manage or when you want a direct sequence of merge passes. The next lesson looks at a different optimization: finding natural runs already present in the input.


