TOPICS
Search

Merge Sort


Merge sort (or collation sort) is a sorting algorithm that sorts smaller groups of items and merges the resulting ordered lists into a single ordered list (Knuth 1998, p. 158). In the two-way version, the items are split into two groups, each group is sorted recursively, and the two ordered lists are merged. Knuth (1998, p. 159) credits John von Neumann with proposing merge sort in 1945. Variants include natural two-way, straight two-way, and list merge sorts.

For balanced two-way merge sort, the maximum number of comparisons b(n) needed to sort n distinct elements is

 b(n)=1+kn-2^k,

where k=|_log_2n_|+1 and |_x_| is the floor function (Steinhaus 1999, pp. 55-56). Equivalently,

 b(n)=sum_(j=1)^n[log_2j],

where [x] is the ceiling function, giving 0, 1, 3, 5, 8, 11, 14, 17, 21, 25, 29, ... (OEIS A001855) for n=1, 2, ....

The different sequence 0, 1, 3, 5, 7, 10, 13, 16, 19, 22, 26, 30, ... (OEIS A001768) gives the maximum number of comparisons used by Ford-Johnson merge-insertion sort (Ford and Johnson 1959), which combines merging with insertion sorting. It does not give the comparison count for ordinary merge sort.


See also

Sorting

Explore with Wolfram|Alpha

References

Ford, L. R. Jr. and Johnson, S. M. "A Tournament Problem." Amer. Math. Monthly 66, 387-389, 1959. https://doi.org/10.1080/00029890.1959.11989306.Knuth, D. E. "Sorting by Merging." §5.2.4 in The Art of Computer Programming, Vol. 3: Sorting and Searching, 2nd ed. Reading, MA: Addison-Wesley, pp. 158-168, 1998.Sloane, N. J. A. Sequences A001768/M2408 and A001855/M2433 in "The On-Line Encyclopedia of Integer Sequences."Steinhaus, H. Mathematical Snapshots, 3rd ed. New York: Dover, 1999.Woodall, A. D. "Algorithm 45: An Internal Sorting Procedure Using a Two-Way Merge." Comput. J. 13, 110-111, 1970.Woodrum, L. J. "Internal Sorting with Minimal Comparing." IBM Systems J. 8, 189-203, 1969.

Referenced on Wolfram|Alpha

Merge Sort

Cite this as:

Weisstein, Eric W. "Merge Sort." From MathWorld--A Wolfram Resource. https://mathworld.wolfram.com/MergeSort.html

Subject classifications