Algorithms And Data Structures Codexery

Merge sort

A stable, divide-and-conquer sorting algorithm invented by John von Neumann.

Last updated

Merge sort is a comparison-based sorting algorithm that uses a divide-and-conquer approach.[1][2] The algorithm was invented by John von Neumann in 1945, with a detailed description appearing in a 1948 report by Goldstine and von Neumann.[2] When two elements in a merge sort are equal, their original order is maintained in the output by the majority of merge sort implementations.[2]

Key facts and history

Merge sort has an average and worst-case performance of O(n log n) comparisons when sorting n objects.[3] The recurrence relation T(n) = 2T(n/2) + n describes the running time, where n steps are taken to merge two lists of half the original size.[3] In the worst case, merge sort uses approximately 39% fewer comparisons than quicksort does in its average case.[4] Merge sort's most common implementation does not sort in place, requiring memory for the sorted output.[3]

A natural merge sort exploits naturally occurring sorted runs in the input, unlike bottom-up merge sort which assumes each run is one item long.[5] A ping-pong merge merges four blocks at a time instead of two, reducing the total number of moves by half.[6] An early public domain implementation of a four-at-once merge was by WikiSort in 2014.[6][7] WikiSort is a stable merge sort based on "Ratio based stable in-place merging" by Pok-Son Kim and Arne Kutzner.[7]

Quadsort is a stable bottom-up adaptive branchless merge sort that starts with an analyzer to detect ordered data.[8]

What the research sources say

The quadsort analyzer examines the array 8 elements at a time, using a method that should result in 0.2 branch mispredictions on random data on average.[8] Reverse-order data can be sorted using n comparisons and n moves through run detection and a reversal routine.[8] A parity merge can be fully unrolled and is suitable for branchless optimizations to speed up sorting of random data.[8] The cross merge, which merges four items at a time, gives a decent performance gain for both ordered and random data.[8]

The simplest parallel merge sort implementation outperforms both STL sort and stable_sort for arrays larger than about 10K elements.[9] A hybrid parallel merge sort using insertion sort for sub-arrays of 48 elements or less accelerated the algorithm by over 2X for all array sizes.[9] The parallel merge sort algorithm achieves a high degree of parallelism, Θ(n/lg² n), and is stable through the use of a stable sort as the recursion base case.[9]

References

  1. Wikipedia, "Merge sort" (CC BY-SA 4.0), which cites: Goodrich, "Data structures and algorithms in Python", Wiley, 2013, p. 538–549 (original not read by us) en.wikipedia.org
  2. Wikipedia, "Merge sort" (CC BY-SA 4.0), which cites: ; Katajainen, "Algorithms and Complexity", March 1997, p. 217–228 (we also read that source) en.wikipedia.org
  3. Wikipedia, "Merge sort" (CC BY-SA 4.0) en.wikipedia.org
  4. Wikipedia, "Merge sort" (CC BY-SA 4.0), which cites: Jayalakshmi, "Data structure using C++", Firewall Media, 2007 (original not read by us) en.wikipedia.org
  5. Wikipedia, "Merge sort" (CC BY-SA 4.0), which cites: Powers, "DCS Technical Report 8313", Department of Computer Science, University of New South Wales, 1983 (original not read by us) en.wikipedia.org
  6. Wikipedia, "Merge sort" (CC BY-SA 4.0), which cites: "WikiSort. Fast and stable sort algorithm that uses O(1) memory. Public domain.", GitHub, 14 Apr 2014; Chandramouli, "Patience is a Virtue: Revisiting Merge and Sort on Modern Processors", 2014; "Quadsort is a branchless stable adaptive merge sort.", GitHub, 8 Jun 2022 (we also read that source) en.wikipedia.org
  7. "WikiSort. Fast and stable sort algorithm that uses O(1) memory. Public domain.", GitHub, 14 Apr 2014. facts only, no text reused. github.com
  8. "Quadsort is a branchless stable adaptive merge sort.", GitHub, 8 Jun 2022. facts only, no text reused. github.com
  9. Victor J. Duvanenko "Parallel Merge Sort" Dr. Dobb's Journal & blog and GitHub repo C++ implementation [https://github.com/DragonSpit/ParallelAlgorithms. facts only, no text reused. duvanenko.tech.blog

Each numbered sentence above was checked against the source it cites before publication. Sentences citing Wikipedia adapt text from the English Wikipedia article Merge sort (contributors), used under CC BY-SA 4.0; this entry is available under the same licence. Other sources were used for facts only; no text or images are copied from them.

More in Algorithms And Data Structures

Sources

Compiled from Wikipedia and the sources listed below. Text from Wikipedia is available under CC BY-SA 4.0; this entry is adapted from it.

Spotted an error? Know more?

Reader corrections go straight into our review queue. Suggest an edit · How this site is sourced

Comments

Loading…
Open in the interactive codex →