This article is part of a series adapted and expanded from my Medium article, published August 5, 2024, and my merge sort repository.
This lesson compares iterative pairwise merging with polyphase merging, two ways to combine sorted runs.
Iterative two-way merging
Instead of using recursive calls, repeatedly merge adjacent pairs of sorted runs. If there are k initial runs containing n elements in total, balanced pairwise merging has at most ceil(log₂ k) passes. Each pass processes at most n elements, giving O(n log k) work for k > 1. If one growing result is merged with each next run in sequence, elements may be processed many more times, with O(nk) as an upper bound.
For an array, bottom-up merge sort starts with runs of length one, then merges runs of length two, four, eight, and so on. Handle a leftover run by carrying it into the next pass.
Polyphase merge
Polyphase merge is an external sorting strategy that distributes runs unevenly among a small number of storage streams. It merges runs from input streams into an output stream, then changes the roles of the streams as an input becomes empty. The goal is to make effective use of limited drives without redistributing every run after each pass.
With three tapes, two can begin as inputs and the third as output. Once an input tape is exhausted, it becomes the next output and the other two become inputs. A suitable initial run distribution, sometimes including empty placeholder runs, is needed for a full schedule. Whether the result is stable depends on the merge and run-ordering rules; polyphase scheduling alone does not guarantee stability.

Follow the tape animation step by step
This animation illustrates a merge pass using three tapes. Tape A and Tape B are inputs; Tape C begins as the empty output. The values on the input tapes are already sorted.
- Initial runs: Tape A holds 1, 4, 7 and Tape B holds 2, 5, 8. Tape C is empty.
- Compare the current heads: compare 1 and 2, write 1 to C, then compare 4 and 2 and write 2. Repeat to write 4 and 5. Tape C now contains the sorted prefix 1, 2, 4, 5.
- One input has only one value left: A has 7 and B has 8. Write 7, then 8. The output run on C is now 1, 2, 4, 5, 7, 8.
- Rotate tape roles: after the inputs are exhausted, a tape can take a different role in a later pass. The animation's last frame shows the completed run; a full polyphase schedule tracks how many runs each tape holds before choosing the next roles.
The animation focuses on the tape movement. Iterative two-way merging uses the same compare-and-copy operation but typically represents its runs in arrays and repeats pairwise passes.

Choosing a merge strategy
Use ordinary merge sort for a stable, predictable in-memory sort when the extra buffer is acceptable. Consider external merging for files larger than available memory. For multiple runs, compare the number of merge passes with the cost of buffers, open files, and storage operations. For almost-sorted input, natural merge sort can detect existing ordered runs and merge them rather than always starting from single elements.
References
The original article drew on these resources:
- Merge sort and external sorting, GeeksforGeeks.
- Polyphase merge sort, GeeksforGeeks.
- External sorting lecture, Robert Siegfried, Adelphi University.
- Parallel Merge Sort with Load Balancing, M. Jeon and D. Kim.
- External sorting slides, S. Limthongkul.
- Merge sort: algorithm, source code, time complexity, Sven Woltmann.
- Data structure and algorithm: merge sort notes, Ta.
- Wikipedia: merge sort, two-way merge, and polyphase merge sort.
- Abdul Bari: Merge Sort Algorithm and Two-way Merge Sort: Iterative Method.
- Natural merge sort illustration, BillowDev, credited as CC BY 4.0 in the original article. The illustration in this edition is newly drawn.



