Merge-insertion sort


In computer science, merge-insertion sort or the Ford–Johnson algorithm is a comparison sorting algorithm published in 1959 by L. R. Ford Jr. and Selmer M. Johnson. It uses fewer comparisons in the worst case than the best previously known algorithms, binary insertion sort and merge sort, and for 20 years it was the sorting algorithm with the fewest known comparisons. Although not of practical significance, it remains of theoretical interest in connection with the problem of sorting with a minimum number of comparisons. The same algorithm may have also been independently discovered by Stanisław Trybuła and Czen Ping.

Algorithm

Merge-insertion sort performs the following steps, on an input of elements:
  1. Group the elements of into pairs of elements, arbitrarily, leaving one element unpaired if there is an odd number of elements.
  2. Perform comparisons, one per pair, to determine the larger of the two elements in each pair.
  3. Recursively sort the larger elements from each pair, creating a sorted sequence of of the input elements, in ascending order.
  4. Insert at the start of the element that was paired with the first and smallest element of.
  5. Insert the remaining elements of into, one at a time, with a specially chosen insertion ordering described below. Use binary search in subsequences of to determine the position at which each element should be inserted.
The algorithm is designed to take advantage of the fact that the binary searches used to insert elements into are most efficient when the length of the subsequence that is searched is one less than a power of two. This is because, for those lengths, all outcomes of the search use the same number of comparisons as each other. To choose an insertion ordering that produces these lengths, consider the sorted sequence after step 4 of the outline above,
and let denote the th element of this sorted sequence. Thus,
where each element with is paired with an element that has not yet been inserted. If is odd, the remaining unpaired element should also be numbered as with larger than the indexes of the paired elements.
Then, the final step of the outline above can be expanded into the following steps:
Let denote the number of comparisons that merge-insertion sort makes, in the worst case, when sorting elements.
This number of comparisons can be broken down as the sum of three terms:
In the third term, the worst-case number of comparisons for the elements in the first group is two, because each is inserted into a subsequence of of length at most three. First, is inserted into the three-element subsequence. Then, is inserted into some permutation of the three-element subsequence, or in some cases into the two-element subsequence. Similarly, the elements and of the second group are each inserted into a subsequence of length at most seven, using three comparisons. More generally, the worst-case number of comparisons for the elements in the th group is, because each is inserted into a subsequence of length at most. By summing the number of comparisons used for all the elements and solving the resulting recurrence relation,
this analysis can be used to compute the values of, giving the formula
or, in closed form,
For the numbers of comparisons are

Relation to other comparison sorts

The algorithm is called merge-insertion sort because the initial comparisons that it performs before its recursive call are the same as the initial comparisons of merge sort,
while the comparisons that it performs after the recursive call follow the same principle as insertion sort. In this sense, it is a hybrid algorithm that combines both merge sort and insertion sort.
For small inputs its numbers of comparisons equal the lower bound on comparison sorting of. However, for larger inputs the number of comparisons made by the merge-insertion algorithm is bigger than this lower bound.
Merge-insertion sort also performs fewer comparisons than the sorting numbers, which count the comparisons made by binary insertion sort or merge sort in the worst case. The sorting numbers fluctuate between and, with the same leading term but a worse constant factor in the lower-order linear term.
Merge-insertion sort is the sorting algorithm with the minimum possible comparisons for items whenever or, and it has the fewest comparisons known for.
For 20 years, merge-insertion sort was the sorting algorithm with the fewest comparisons known for all input lengths.
However, in 1979 Glenn Manacher published another sorting algorithm that used even fewer comparisons, for large enough inputs.
It remains unknown exactly how many comparisons are needed for sorting, for all, but Manacher's algorithm
and later record-breaking sorting algorithms have all used modifications of the merge-insertion sort ideas.