Bottom-Up Merge Sort: Mechanism, Complexity Analysis & Interactive Visualizer
Bottom-Up Merge Sort
Same merges, no recursion. Walk over the array in runs of size 1, then 2, then 4, doubling each pass until the whole array is one run.
How it works
The recursive Merge Sort splits then merges. Bottom-Up Merge Sort flips this around: it starts assuming runs of length 1 are already sorted, then merges adjacent runs into runs of length 2, then 4, and so on.
- Start with run width
w = 1. - Walk the array: merge each pair of adjacent runs of width
winto one run of width2w. - Double
wand repeat untilw ≥ n.
The total work and stability are identical to top-down Merge Sort. What changes: no recursion, no function-call stack, and slightly better cache behavior because passes are sequential. This formulation is preferred for external/disk-based merge sort.
Complexity
Interactive visualization
Pseudocode
procedure bottomUpMergeSort(A): n ← length(A) w ← 1 while w < n: for lo from 0 to n − 1 step 2w: mid ← min(lo + w − 1, n − 1) hi ← min(lo + 2w − 1, n − 1) merge(A, lo, mid, hi) w ← 2w
JavaScript implementation
function bottomUpMergeSort(arr) { const n = arr.length; for (let w = 1; w < n; w *= 2) { for (let lo = 0; lo < n; lo += 2 * w) { const m = Math.min(lo + w - 1, n - 1); const hi = Math.min(lo + 2 * w - 1, n - 1); merge(arr, lo, m, hi); } } return arr; } function merge(arr, lo, m, hi) { const aux = arr.slice(lo, hi + 1); let i = 0, j = m - lo + 1, k = lo; while (i <= m - lo && j <= hi - lo) { if (aux[i] <= aux[j]) { arr[k] = aux[i]; i++; } else { arr[k] = aux[j]; j++; } k++; } while (i <= m - lo) { arr[k] = aux[i]; i++; k++; } while (j <= hi - lo) { arr[k] = aux[j]; j++; k++; } }
Pros and cons
Pros
- No recursion — uses constant stack space.
- Identical asymptotic behavior to top-down Merge Sort.
- Excellent sequential I/O pattern — ideal for external sorting.
- Easy to parallelize per pass.
Cons
- Still needs O(n) auxiliary buffer.
- Slightly worse cache behavior than top-down on in-memory arrays.
- Not in-place.
When to use it
- External merge sort on disk-resident data.
- Stack-limited environments (embedded, scripted languages with low recursion depth).
- Pipeline-friendly data processing systems.
Related reading
Sleep Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A unique concurrency-based sorting algorithm: spawns a separate thread for each element, sleeping for a duration proportional to the element value before writing it to the output.
By Md. Harun-Or-Roshid
Cube Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A parallel dimension sorting algorithm: organizes elements in a 3D coordinate cube and sorts vectors along the height, width, and depth axes sequentially to achieve sorted order.
By Md. Harun-Or-Roshid
Patience Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A card-game inspired sorting algorithm: distributes elements onto a series of piles such that each pile's top is \(\ge\) the incoming value, then performs a multi-way merge across pile tops.
By Md. Harun-Or-Roshid
Library Sort: Mechanism, Complexity Analysis & Interactive Visualizer
A gapped variation of Insertion Sort: leaves empty slots (gaps) in the array, allowing elements to be inserted in \(O(1)\) shift time on average by avoiding massive shifts.
By Md. Harun-Or-Roshid