Merge sort
From WikiTech, the free technology encyclopedia
Divide-and-conquer sorting algorithm (1945)
5 sources from Fermat's Library, Wolfram MathWorld, NIST Dictionary of Algorithms and Data Structures and 2 more. Checked by ByteExplained editors on 5 December 2026. How we check facts
Merge sort is a sorting algorithm that puts a list of items in order by splitting it into smaller parts, sorting those parts and then merging the sorted parts back together. It is one of the oldest computer algorithms, proposed by John von Neumann in 1945.[1]
History
Von Neumann wrote a sorting program by hand in 1945 while refining the design of the EDVAC, an early stored-program computer.[1] Rather than solving a numerical problem, the program was meant to test whether the proposed instruction set could handle logical tasks such as rearranging data.[1] It is thought to be the earliest surviving program written for a stored-program digital computer, and the method it uses is the one now known as merge sort.[1] The computer scientist Donald Knuth analysed the program in a 1970 article, "Von Neumann's First Computer Program", in the journal Computing Surveys, and in his book The Art of Computer Programming he credits von Neumann with proposing merge sort in 1945.[1][2]
How it works
The usual two-way version divides the items into two groups, sorts each group by applying the same method to it (a technique called recursion), and then merges the two sorted groups into one ordered list.[3] Merging is straightforward: the two sorted halves are copied to a working area and combined back into the original list in order.[4] A "bottom-up" version avoids recursion by first merging single items into pairs, then pairs into groups of four, and so on until the whole list is sorted.[4]
Merge sort is guaranteed to sort n items in time proportional to n log n, whatever the starting order of the input, and no sorting method that works by comparing items can guarantee to do much better.[3][4] Its main drawback is that the standard version needs extra working memory in proportion to the number of items.[4] Practical versions often switch to a simpler method for very small groups, which can make them 10 to 15 per cent faster.[4] Specialised variants include k-way and polyphase merge sorts.[3]
Use
Merge sort appears in real-world software libraries. For example, the documentation for the Java programming language's standard library names merge sort when describing its method for sorting objects, which must be stable: items that compare as equal keep their original order.[5]
See also
References
- ^ Von Neumann's First Computer Program (Donald E. Knuth, Computing Surveys, 1970). Fermat's Library. fermatslibrary.com
- ^ Merge Sort. Wolfram MathWorld. mathworld.wolfram.com
- ^ merge sort. NIST Dictionary of Algorithms and Data Structures. 2023-04-06. xlinux.nist.gov
- ^ Mergesort (Algorithms, 4th Edition). Princeton University. algs4.cs.princeton.edu
- ^ Arrays (Java Platform SE 8). Oracle. docs.oracle.com





