Merge algorithm
Merge algorithms are a family of algorithms that take multiple sorted lists as input and produce a single list as output, containing all the elements of the inputs lists in sorted order. These algorithms are used as subroutines in various sorting algorithms, most famously merge sort.
Merge algorithms are a family of algorithms that take multiple sorted lists as input and produce a single list as output, containing all the elements of the inputs lists in sorted order. These algorithms are used as subroutines in various sorting algorithms, most famously merge sort. C := new empty list while A is not empty and B is not empty do if head(A) ≤ head(B) then append head(A) to C drop the head of A else append head(B) to C drop the head of B // By now, either A or B is empty. It remains to empty the other input list. while A is not empty do append head(A) to C drop the head of A while B is not empty do append head(B) to C drop the head of B return C When the inputs are linked lists, this algorithm can be implemented to use only a constant amount of working space; the pointers in the lists' nodes can be reused for bookkeeping and for constructing the final merged list. In the merge sort algorithm, this subroutine is typically used to merge two sub-arrays , of a single array . This can be done by copying the sub-arrays into a temporary array, then applying the merge algorithm above. The allocation of a temporary array can be avoided, but at the expense of speed and programming ease. Various in-place merge algorithms have been devised, sometimes sacrificing the linear-time bound to produce an algorithm; see for discussion. ==K-way merging== -way merging generalizes binary merging to an arbitrary number of sorted input lists. Applications of -way merging arise in various sorting algorithms, including patience sorting and an external sorting algorithm that divides its input into blocks that fit in memory, sorts these one by one, then merges these blocks. Several solutions to this problem exist. A naive solution is to do a loop over the lists to pick off the minimum element each time, and repeat this loop until all lists are empty: Input: a list of lists. While any of the lists is non-empty: Loop over the lists to find the one with the minimum first element. Output the minimum element and remove it from its list. In the worst case, this algorithm performs element comparisons to perform its work if there are a total of elements in the lists. It can be improved by storing the lists in a priority queue (min-heap) keyed by their first element: Build a min-heap of the lists, using the first element as the key. While any of the lists is non-empty: Let . Output the first element of list and remove it from its list. Re-heapify . Searching for the next smallest element to be output (find-min) and restoring heap order can now be done in time (more specifically, comparisons ), and the full problem can be solved in time (approximately comparisons). A third algorithm for the problem is a divide and conquer solution that builds on the binary merge algorithm: If , output the single input list. If , perform a binary merge. Else, recursively merge the first lists and the final lists, then binary merge these. When the input lists to this algorithm are ordered by length, shortest first, it requires fewer than comparisons, i.e., less than half the number used by the heap-based algorithm; in practice, it may be about as fast or slow as the heap-based algorithm. let m = j - i, n = ℓ - k if m \frac 3 4 n since the array with more elements is perfectly split in half. Adding the \Theta\left( \log(n)\right) cost of the Binary Search, we obtain this recurrence as an upper bound: T_{\infty}^\text{merge}(n) = T_{\infty}^\text{merge}\left(\frac {3} {4} n\right) + \Theta\left( \log(n)\right) The solution is T_{\infty}^\text{merge}(n) = \Theta\left(\log(n)^2\right) , meaning that it takes that much time on an ideal machine with an unbounded number of processors. Note: The routine is not stable: if equal items are separated by splitting and , they will become interleaved in ; also swapping and will destroy the order, if equal items are spread among both input arrays. As a result, when used for sorting, this algorithm produces a sort that is not stable. ==External links== High Performance Implementation of Parallel and Serial Merge in C# with source in GitHub and in C++ GitHub
References
- A High-Performance and Cost-Effective Hardware Merge Sorter without Feedback Datapath · retrieved 2026-11-20 · rev 1378002457
Cite this reference
MLA "A High-Performance and Cost-Effective Hardware Merge Sorter without Feedback Datapath." Saitoh, Makoto; Elsayed, Elsayed A. 2026-11-20. https://en.wikipedia.org/wiki/Doi_(identifier). Accessed 24 November 2026.
Chicago "A High-Performance and Cost-Effective Hardware Merge Sorter without Feedback Datapath." Saitoh, Makoto; Elsayed, Elsayed A. 2026-11-20. https://en.wikipedia.org/wiki/Doi_(identifier).
APA Saitoh, Makoto; Elsayed, Elsayed A. (2026). A High-Performance and Cost-Effective Hardware Merge Sorter without Feedback Datapath. Retrieved November 24, 2026, from https://en.wikipedia.org/wiki/Doi_(identifier)
- std:merge · retrieved 2018-04-27 · rev 1378002457
Cite this reference
MLA "std:merge." 2018-04-27. http://en.cppreference.com/w/cpp/algorithm/merge. Accessed 24 November 2026.
Chicago "std:merge." 2018-04-27. http://en.cppreference.com/w/cpp/algorithm/merge.
APA encyclopegia.org. (2018). std:merge. Retrieved November 24, 2026, from http://en.cppreference.com/w/cpp/algorithm/merge
- heapq — Heap queue algorithm — Python 3.10.1 documentation · retrieved 2026-11-20 · rev 1378002457
Cite this reference
MLA "heapq — Heap queue algorithm — Python 3.10.1 documentation." "heapq — Heap queue algorithm — Python 3. 2026-11-20. https://docs.python.org/library/heapq.html#heapq.merge. Accessed 24 November 2026.
Chicago "heapq — Heap queue algorithm — Python 3.10.1 documentation." "heapq — Heap queue algorithm — Python 3. 2026-11-20. https://docs.python.org/library/heapq.html#heapq.merge.
APA "heapq — Heap queue algorithm — Python 3. (2026). heapq — Heap queue algorithm — Python 3.10.1 documentation. Retrieved November 24, 2026, from https://docs.python.org/library/heapq.html#heapq.merge
- Practical in-place mergesort · retrieved 2026-11-20 · rev 1378002457
Cite this reference
MLA "Practical in-place mergesort." Katajainen, Jyrki; Pasanen, Tomi; Teuhola, Jukka (1996). 2026-11-20. https://dl.acm.org/doi/10.5555/642136.642138. Accessed 24 November 2026.
Chicago "Practical in-place mergesort." Katajainen, Jyrki; Pasanen, Tomi; Teuhola, Jukka (1996). 2026-11-20. https://dl.acm.org/doi/10.5555/642136.642138.
APA Katajainen, Jyrki; Pasanen, Tomi; Teuhola, Jukka (1996). (2026). Practical in-place mergesort. Retrieved November 24, 2026, from https://dl.acm.org/doi/10.5555/642136.642138
- Kim, Pok-Son; Kutzner, Arne (2004). Stable Minimum Storage Merging by Symmetric Comparisons . European Symp. Algorithms. Lecture Notes in Computer Science. Vol. 3221. pp. 714– 723. doi : 10.1007/978-3-540-30140-0_63 . ISBN 978-3-540-23025-0 . Wikipedia revision 1378002457, retrieved 2026-11-21 · retrieved 2026-11-20 · rev 1378002457
Cite this reference
MLA "Kim, Pok-Son; Kutzner, Arne (2004). Stable Minimum Storage Merging by Symmetric Comparisons . European Symp. Algorithms. Lecture Notes in Computer Science. Vol. 3221. pp. 714– 723. doi : 10.1007/978-3-540-30140-0_63 . ISBN 978-3-540-23025-0 . Wikipedia revision 1378002457, retrieved 2026-11-21." Kim, Pok-Son; Kutzner, Arne (2004). 2026-11-20. https://en.wikipedia.org/wiki/Doi_(identifier). Accessed 24 November 2026.
Chicago "Kim, Pok-Son; Kutzner, Arne (2004). Stable Minimum Storage Merging by Symmetric Comparisons . European Symp. Algorithms. Lecture Notes in Computer Science. Vol. 3221. pp. 714– 723. doi : 10.1007/978-3-540-30140-0_63 . ISBN 978-3-540-23025-0 . Wikipedia revision 1378002457, retrieved 2026-11-21." Kim, Pok-Son; Kutzner, Arne (2004). 2026-11-20. https://en.wikipedia.org/wiki/Doi_(identifier).
APA Kim, Pok-Son; Kutzner, Arne (2004). (2026). Kim, Pok-Son; Kutzner, Arne (2004). Stable Minimum Storage Merging by Symmetric Comparisons . European Symp. Algorithms. Lecture Notes in Computer Science. Vol. 3221. pp. 714– 723. doi : 10.1007/978-3-540-30140-0_63 . ISBN 978-3-540-23025-0 . Wikipedia revision 1378002457, retrieved 2026-11-21. Retrieved November 24, 2026, from https://en.wikipedia.org/wiki/Doi_(identifier)
- Chandramouli, Badrish; Goldstein, Jonathan (2014). Patience is a Virtue: Revisiting Merge and Sort on Modern Processors . SIGMOD/PODS. Wikipedia revision 1378002457, retrieved 2026-11-21 · retrieved 2026-11-20 · rev 1378002457
Cite this reference
MLA "Chandramouli, Badrish; Goldstein, Jonathan (2014). Patience is a Virtue: Revisiting Merge and Sort on Modern Processors . SIGMOD/PODS. Wikipedia revision 1378002457, retrieved 2026-11-21." Chandramouli, Badrish; Goldstein, Jonathan (2014). 2026-11-20. https://en.wikipedia.org/w/index.php?oldid=1378002457. Accessed 24 November 2026.
Chicago "Chandramouli, Badrish; Goldstein, Jonathan (2014). Patience is a Virtue: Revisiting Merge and Sort on Modern Processors . SIGMOD/PODS. Wikipedia revision 1378002457, retrieved 2026-11-21." Chandramouli, Badrish; Goldstein, Jonathan (2014). 2026-11-20. https://en.wikipedia.org/w/index.php?oldid=1378002457.
APA Chandramouli, Badrish; Goldstein, Jonathan (2014). (2026). Chandramouli, Badrish; Goldstein, Jonathan (2014). Patience is a Virtue: Revisiting Merge and Sort on Modern Processors . SIGMOD/PODS. Wikipedia revision 1378002457, retrieved 2026-11-21. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1378002457
- a b Cormen, Thomas H. ; Leiserson, Charles E. ; Rivest, Ronald L. ; Stein, Clifford (2009) [1990]. Introduction to Algorithms (3rd ed.). MIT Press and McGraw-Hill. ISBN 0-262-03384-4 . Wikipedia revision 1378002457, retrieved 2026-11-21 · retrieved 2026-11-20 · rev 1378002457
Cite this reference
MLA "a b Cormen, Thomas H. ; Leiserson, Charles E. ; Rivest, Ronald L. ; Stein, Clifford (2009) [1990]. Introduction to Algorithms (3rd ed.). MIT Press and McGraw-Hill. ISBN 0-262-03384-4 . Wikipedia revision 1378002457, retrieved 2026-11-21." a b Cormen, Thomas H. 2026-11-20. https://en.wikipedia.org/wiki/Thomas_H._Cormen. Accessed 24 November 2026.
Chicago "a b Cormen, Thomas H. ; Leiserson, Charles E. ; Rivest, Ronald L. ; Stein, Clifford (2009) [1990]. Introduction to Algorithms (3rd ed.). MIT Press and McGraw-Hill. ISBN 0-262-03384-4 . Wikipedia revision 1378002457, retrieved 2026-11-21." a b Cormen, Thomas H. 2026-11-20. https://en.wikipedia.org/wiki/Thomas_H._Cormen.
APA a b Cormen, Thomas H. (2026). a b Cormen, Thomas H. ; Leiserson, Charles E. ; Rivest, Ronald L. ; Stein, Clifford (2009) [1990]. Introduction to Algorithms (3rd ed.). MIT Press and McGraw-Hill. ISBN 0-262-03384-4 . Wikipedia revision 1378002457, retrieved 2026-11-21. Retrieved November 24, 2026, from https://en.wikipedia.org/wiki/Thomas_H._Cormen
- FLiMS: a Fast Lightweight 2-way Merger for Sorting · retrieved 2026-11-20 · rev 1378002457
Cite this reference
MLA "FLiMS: a Fast Lightweight 2-way Merger for Sorting." a b Papaphilippou, Philippos; Luk, Wayne; Brooks, Chris (2022). 2026-11-20. https://en.wikipedia.org/wiki/ArXiv_(identifier). Accessed 24 November 2026.
Chicago "FLiMS: a Fast Lightweight 2-way Merger for Sorting." a b Papaphilippou, Philippos; Luk, Wayne; Brooks, Chris (2022). 2026-11-20. https://en.wikipedia.org/wiki/ArXiv_(identifier).
APA a b Papaphilippou, Philippos; Luk, Wayne; Brooks, Chris (2022). (2026). FLiMS: a Fast Lightweight 2-way Merger for Sorting. Retrieved November 24, 2026, from https://en.wikipedia.org/wiki/ArXiv_(identifier)
- a b c d Greene, William A. (1993). k-way Merging and k-ary Sorts (PDF) . Proc. 31-st Annual ACM Southeast Conf. pp. 127– 135. Wikipedia revision 1378002457, retrieved 2026-11-21 · retrieved 2026-11-20 · rev 1378002457
Cite this reference
MLA "a b c d Greene, William A. (1993). k-way Merging and k-ary Sorts (PDF) . Proc. 31-st Annual ACM Southeast Conf. pp. 127– 135. Wikipedia revision 1378002457, retrieved 2026-11-21." a b c d Greene, William A. 2026-11-20. http://www.cs.uno.edu/people/faculty/bill/k-way-merge-n-sort-ACM-SE-Regl-1993.pdf. Accessed 24 November 2026.
Chicago "a b c d Greene, William A. (1993). k-way Merging and k-ary Sorts (PDF) . Proc. 31-st Annual ACM Southeast Conf. pp. 127– 135. Wikipedia revision 1378002457, retrieved 2026-11-21." a b c d Greene, William A. 2026-11-20. http://www.cs.uno.edu/people/faculty/bill/k-way-merge-n-sort-ACM-SE-Regl-1993.pdf.
APA a b c d Greene, William A. (2026). a b c d Greene, William A. (1993). k-way Merging and k-ary Sorts (PDF) . Proc. 31-st Annual ACM Southeast Conf. pp. 127– 135. Wikipedia revision 1378002457, retrieved 2026-11-21. Retrieved November 24, 2026, from http://www.cs.uno.edu/people/faculty/bill/k-way-merge-n-sort-ACM-SE-Regl-1993.pdf
- a b Skiena, Steven (2010). The Algorithm Design Manual (2nd ed.). Springer Science+Business Media . p. 123. ISBN 978-1-849-96720-4 . Wikipedia revision 1378002457, retrieved 2026-11-21 · retrieved 2026-11-20 · rev 1378002457
Cite this reference
MLA "a b Skiena, Steven (2010). The Algorithm Design Manual (2nd ed.). Springer Science+Business Media . p. 123. ISBN 978-1-849-96720-4 . Wikipedia revision 1378002457, retrieved 2026-11-21." a b Skiena, Steven (2010). 2026-11-20. https://en.wikipedia.org/wiki/Steven_Skiena. Accessed 24 November 2026.
Chicago "a b Skiena, Steven (2010). The Algorithm Design Manual (2nd ed.). Springer Science+Business Media . p. 123. ISBN 978-1-849-96720-4 . Wikipedia revision 1378002457, retrieved 2026-11-21." a b Skiena, Steven (2010). 2026-11-20. https://en.wikipedia.org/wiki/Steven_Skiena.
APA a b Skiena, Steven (2010). (2026). a b Skiena, Steven (2010). The Algorithm Design Manual (2nd ed.). Springer Science+Business Media . p. 123. ISBN 978-1-849-96720-4 . Wikipedia revision 1378002457, retrieved 2026-11-21. Retrieved November 24, 2026, from https://en.wikipedia.org/wiki/Steven_Skiena
- a b c Kurt Mehlhorn ; Peter Sanders (2008). Algorithms and Data Structures: The Basic Toolbox . Springer. ISBN 978-3-540-77978-0 . Wikipedia revision 1378002457, retrieved 2026-11-21 · retrieved 2026-11-20 · rev 1378002457
Cite this reference
MLA "a b c Kurt Mehlhorn ; Peter Sanders (2008). Algorithms and Data Structures: The Basic Toolbox . Springer. ISBN 978-3-540-77978-0 . Wikipedia revision 1378002457, retrieved 2026-11-21." a b c Kurt Mehlhorn ; Peter Sanders (2008). 2026-11-20. https://en.wikipedia.org/wiki/Kurt_Mehlhorn. Accessed 24 November 2026.
Chicago "a b c Kurt Mehlhorn ; Peter Sanders (2008). Algorithms and Data Structures: The Basic Toolbox . Springer. ISBN 978-3-540-77978-0 . Wikipedia revision 1378002457, retrieved 2026-11-21." a b c Kurt Mehlhorn ; Peter Sanders (2008). 2026-11-20. https://en.wikipedia.org/wiki/Kurt_Mehlhorn.
APA a b c Kurt Mehlhorn ; Peter Sanders (2008). (2026). a b c Kurt Mehlhorn ; Peter Sanders (2008). Algorithms and Data Structures: The Basic Toolbox . Springer. ISBN 978-3-540-77978-0 . Wikipedia revision 1378002457, retrieved 2026-11-21. Retrieved November 24, 2026, from https://en.wikipedia.org/wiki/Kurt_Mehlhorn
- Parallel Merge · retrieved 2026-11-20 · rev 1378002457
Cite this reference
MLA "Parallel Merge." Victor J. 2026-11-20. http://www.drdobbs.com/parallel/parallel-merge/229204454. Accessed 24 November 2026.
Chicago "Parallel Merge." Victor J. 2026-11-20. http://www.drdobbs.com/parallel/parallel-merge/229204454.
APA Victor J. (2026). Parallel Merge. Retrieved November 24, 2026, from http://www.drdobbs.com/parallel/parallel-merge/229204454
- Katajainen, Jyrki. Practical in-place mergesort . 1996. · retrieved 2026-10-01 · rev 1378002457
Cite this reference
MLA "Katajainen, Jyrki. Practical in-place mergesort . 1996." Katajainen, Jyrki. 2026-10-01. https://en.wikipedia.org/w/index.php?oldid=1378002457. Accessed 24 November 2026.
Chicago "Katajainen, Jyrki. Practical in-place mergesort . 1996." Katajainen, Jyrki. 2026-10-01. https://en.wikipedia.org/w/index.php?oldid=1378002457.
APA Katajainen, Jyrki. (2026). Katajainen, Jyrki. Practical in-place mergesort . 1996. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1378002457
- Greene, William A. k-way Merging and k-ary Sorts . 1993. · retrieved 2026-10-01 · rev 1378002457
Cite this reference
MLA "Greene, William A. k-way Merging and k-ary Sorts . 1993." Greene, William A. 2026-10-01. https://en.wikipedia.org/w/index.php?oldid=1378002457. Accessed 24 November 2026.
Chicago "Greene, William A. k-way Merging and k-ary Sorts . 1993." Greene, William A. 2026-10-01. https://en.wikipedia.org/w/index.php?oldid=1378002457.
APA Greene, William A. (2026). Greene, William A. k-way Merging and k-ary Sorts . 1993. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1378002457
- Algorithms and Data Structures: The Basic Toolbox . 2008. · retrieved 2026-10-01 · rev 1378002457
Cite this reference
MLA "Algorithms and Data Structures: The Basic Toolbox . 2008." Algorithms and Data Structures: The Basic Toolbox. 2026-10-01. https://en.wikipedia.org/w/index.php?oldid=1378002457. Accessed 24 November 2026.
Chicago "Algorithms and Data Structures: The Basic Toolbox . 2008." Algorithms and Data Structures: The Basic Toolbox. 2026-10-01. https://en.wikipedia.org/w/index.php?oldid=1378002457.
APA Algorithms and Data Structures: The Basic Toolbox. (2026). Algorithms and Data Structures: The Basic Toolbox . 2008. Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1378002457
- Parallel Merge . · retrieved 2026-10-01 · rev 1378002457
Cite this reference
MLA "Parallel Merge ." 2026-10-01. https://en.wikipedia.org/w/index.php?oldid=1378002457. Accessed 24 November 2026.
Chicago "Parallel Merge ." 2026-10-01. https://en.wikipedia.org/w/index.php?oldid=1378002457.
APA encyclopegia.org. (2026). Parallel Merge . Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1378002457
- that improved the hardware utilization and performance by only requiring \log_2(P)+1 pipeline stages of math|P/2 compare-and-swap units to merge with a parallelism of math|P elements p . · retrieved 2026-10-01 · rev 1378002457
Cite this reference
MLA "that improved the hardware utilization and performance by only requiring \log_2(P)+1 pipeline stages of math|P/2 compare-and-swap units to merge with a parallelism of math|P elements p ." that improved the hardware utilization and performance by only requiring \log_2(P)+1 pipeline stages of math|P/2 compare. 2026-10-01. https://en.wikipedia.org/w/index.php?oldid=1378002457. Accessed 24 November 2026.
Chicago "that improved the hardware utilization and performance by only requiring \log_2(P)+1 pipeline stages of math|P/2 compare-and-swap units to merge with a parallelism of math|P elements p ." that improved the hardware utilization and performance by only requiring \log_2(P)+1 pipeline stages of math|P/2 compare. 2026-10-01. https://en.wikipedia.org/w/index.php?oldid=1378002457.
APA that improved the hardware utilization and performance by only requiring \log_2(P)+1 pipeline stages of math|P/2 compare. (2026). that improved the hardware utilization and performance by only requiring \log_2(P)+1 pipeline stages of math|P/2 compare-and-swap units to merge with a parallelism of math|P elements p . Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1378002457
- heapq — Heap queue algorithm — Python 3.10.1 documentation . · retrieved 2026-10-01 · rev 1378002457
Cite this reference
MLA "heapq — Heap queue algorithm — Python 3.10.1 documentation ." heapq — Heap queue algorithm — Python 3. 2026-10-01. https://en.wikipedia.org/w/index.php?oldid=1378002457. Accessed 24 November 2026.
Chicago "heapq — Heap queue algorithm — Python 3.10.1 documentation ." heapq — Heap queue algorithm — Python 3. 2026-10-01. https://en.wikipedia.org/w/index.php?oldid=1378002457.
APA heapq — Heap queue algorithm — Python 3. (2026). heapq — Heap queue algorithm — Python 3.10.1 documentation . Retrieved November 24, 2026, from https://en.wikipedia.org/w/index.php?oldid=1378002457
Cite this article
MLA
"Merge algorithm." encyclopegia.org, 2026, https://encyclopegia.org/wiki/merge-algorithm. Accessed 24 November 2026.
Chicago
"Merge algorithm." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/merge-algorithm.
APA
encyclopegia.org. (2026). Merge algorithm. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/merge-algorithm
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON