e01.org

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

  1. ^ Saitoh, Makoto; Elsayed, Elsayed A.; Chu, Thiem Van; Mashimo, Susumu; Kise, Kenji (April 2018). "A High-Performance and Cost-Effective Hardware Merge Sorter without Feedback Datapath". 2018 IEEE 26th Annual International Symposium on Field-Programmable Custom Computing Machines (FCCM) . pp. 197– 204. doi : 10.1109/FCCM.2018.00038 . ISBN 978-1-5386-5522-1 . S2CID 52195866 . Wikipedia revision 1378002457, retrieved 2026-11-21 · 1795215600.0
  2. ^ "std:merge" . cppreference.com. 2018-01-08 . Retrieved 2018-04-28 . Wikipedia revision 1378002457, retrieved 2026-11-21 · 1524870000.0
  3. ^ "heapq — Heap queue algorithm — Python 3.10.1 documentation" . Wikipedia revision 1378002457, retrieved 2026-11-21 · 1795215600.0
  4. ^ Katajainen, Jyrki; Pasanen, Tomi; Teuhola, Jukka (1996). "Practical in-place mergesort" . Nordic J. Computing . 3 (1): 27– 40. Wikipedia revision 1378002457, retrieved 2026-11-21 · 1795215600.0
  5. ^ 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 · 1795215600.0
  6. ^ 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 · 1795215600.0
  7. ^ 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 · 1795215600.0
  8. ^ a b Papaphilippou, Philippos; Luk, Wayne; Brooks, Chris (2022). "FLiMS: a Fast Lightweight 2-way Merger for Sorting". IEEE Transactions on Computers : 1– 12. arXiv : 2112.05607 . doi : 10.1109/TC.2022.3146509 . hdl : 10044/1/95271 . S2CID 245669103 . Wikipedia revision 1378002457, retrieved 2026-11-21 · 1795215600.0
  9. ^ 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 · 1795215600.0
  10. ^ 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 · 1795215600.0
  11. ^ 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 · 1795215600.0
  12. ^ Victor J. Duvanenko (2011), "Parallel Merge" , Dr. Dobb's Journal Wikipedia revision 1378002457, retrieved 2026-11-21 · 1795215600.0
  13. Katajainen, Jyrki. Practical in-place mergesort . 1996. · 1790895600.0
  14. Greene, William A. k-way Merging and k-ary Sorts . 1993. · 1790895600.0
  15. Algorithms and Data Structures: The Basic Toolbox . 2008. · 1790895600.0
  16. Parallel Merge . · 1790895600.0
  17. 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 . · 1790895600.0
  18. heapq — Heap queue algorithm — Python 3.10.1 documentation . · 1790895600.0
source: e01 Encyclopedia · licence: CC BY-SA 4.0 · page · source revision

Cite this

"Merge algorithm". 1790931599. e01 Encyclopedia. CC BY-SA 4.0. https://en.wikipedia.org/w/index.php?oldid=1378002457

JSON record · API collection · citation policy