Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Merge-insertion sort

From Wikipedia, the free encyclopedia
Type of comparison sorting algorithm

Incomputer science,merge-insertion sort or theFord–Johnson algorithm is acomparison sorting algorithm published in 1959 byL. R. Ford Jr. andSelmer M. Johnson.[1][2][3][4] It uses fewer comparisons in theworst case than the best previously known algorithms,binary insertion sort andmerge sort,[1] and for 20 years it was the sorting algorithm with the fewest known comparisons.[5] Although not of practical significance, it remains of theoretical interest in connection with the problem of sorting with a minimum number of comparisons.[3] The same algorithm may have also been independently discovered byStanisław Trybuła and Czen Ping.[4]

An animation of themerge-algorithm sorting an array of randomized values.

Algorithm

[edit]

Merge-insertion sort performs the following steps, on an inputX{\displaystyle X} ofn{\displaystyle n} elements:[6]

  1. Group the elements ofX{\displaystyle X} inton/2{\displaystyle \lfloor n/2\rfloor } pairs of elements, arbitrarily, leaving one element unpaired if there is an odd number of elements.
  2. Performn/2{\displaystyle \lfloor n/2\rfloor } comparisons, one per pair, to determine the larger of the two elements in each pair.
  3. Recursively sort then/2{\displaystyle \lfloor n/2\rfloor } larger elements from each pair, creating a sorted sequenceS{\displaystyle S} ofn/2{\displaystyle \lfloor n/2\rfloor } of the input elements, in ascending order, using the merge-insertion sort.
  4. Insert at the start ofS{\displaystyle S} the element that was paired with the first and smallest element ofS{\displaystyle S}.
  5. Insert the remainingn/21{\displaystyle \lceil n/2\rceil -1} elements ofXS{\displaystyle X\setminus S} intoS{\displaystyle S}, one at a time, with a specially chosen insertion ordering described below. Usebinary search in subsequences ofS{\displaystyle S} (as described below) 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 intoS{\displaystyle S} are most efficient (from the point of view of worst case analysis) when the length of the subsequence that is searched is one less than apower of two. This is because, for those lengths, all outcomes of the search use the same number of comparisons as each other.[1] To choose an insertion ordering that produces these lengths, consider the sorted sequenceS{\displaystyle S} after step 4 of the outline above (before inserting the remaining elements),and letxi{\displaystyle x_{i}} denote thei{\displaystyle i}th element of this sorted sequence. Thus,

S=(x1,x2,x3,),{\displaystyle S=(x_{1},x_{2},x_{3},\dots ),}

where each elementxi{\displaystyle x_{i}} withi3{\displaystyle i\geq 3} is paired with an elementyi<xi{\displaystyle y_{i}<x_{i}} that has not yet been inserted. (There are no elementsy1{\displaystyle y_{1}} ory2{\displaystyle y_{2}} becausex1{\displaystyle x_{1}} andx2{\displaystyle x_{2}} were paired with each other.) Ifn{\displaystyle n} is odd, the remaining unpaired element should also be numbered asyi{\displaystyle y_{i}} withi{\displaystyle i} larger than the indexes of the paired elements.Then, the final step of the outline above can be expanded into the following steps:[1][2][3][4]

  • Partition the uninserted elementsyi{\displaystyle y_{i}} into groups with contiguous indexes. There are two elementsy3{\displaystyle y_{3}} andy4{\displaystyle y_{4}} in the first group, and the sums of sizes of every two adjacent groups form a sequence of powers of two. Thus, the sizes of groups are: 2, 2, 6, 10, 22, 42, ...
  • Order the uninserted elements by their groups (smaller indexes to larger indexes), but within each group order them from larger indexes to smaller indexes. Thus, the ordering becomes
y4,y3,y6,y5,y12,y11,y10,y9,y8,y7,y22,y21{\displaystyle y_{4},y_{3},y_{6},y_{5},y_{12},y_{11},y_{10},y_{9},y_{8},y_{7},y_{22},y_{21}\dots }

Analysis

[edit]

LetC(n){\displaystyle C(n)} denote the number of comparisons that merge-insertion sort makes, in the worst case, when sortingn{\displaystyle n} 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 ofS{\displaystyle S} of length at most three. First,y4{\displaystyle y_{4}} is inserted into the three-element subsequence(x1,x2,x3){\displaystyle (x_{1},x_{2},x_{3})}. Then,y3{\displaystyle y_{3}} is inserted into some permutation of the three-element subsequence(x1,x2,y4){\displaystyle (x_{1},x_{2},y_{4})}, or in some cases into the two-element subsequence(x1,x2){\displaystyle (x_{1},x_{2})}. Similarly, the elementsy6{\displaystyle y_{6}} andy5{\displaystyle y_{5}} 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 thei{\displaystyle i}th group isi+1{\displaystyle i+1}, because each is inserted into a subsequence of length at most2i+11{\displaystyle 2^{i+1}-1}.[1][2][3][4] By summing the number of comparisons used for all the elements and solving the resultingrecurrence relation,this analysis can be used to compute the values ofC(n){\displaystyle C(n)}, giving the formula[7]

C(n)=i=1nlog23i4nlog2n1.415n{\displaystyle C(n)=\sum _{i=1}^{n}\left\lceil \log _{2}{\frac {3i}{4}}\right\rceil \approx n\log _{2}n-1.415n}

or, inclosed form,[8]

C(n)=nlog23n42log26n3+log26n2.{\displaystyle C(n)=n{\biggl \lceil }\log _{2}{\frac {3n}{4}}{\biggr \rceil }-{\biggl \lfloor }{\frac {2^{\lfloor \log _{2}6n\rfloor }}{3}}{\biggr \rfloor }+{\biggl \lfloor }{\frac {\log _{2}6n}{2}}{\biggr \rfloor }.}

Forn=1,2,{\displaystyle n=1,2,\dots } the numbers of comparisons are[1]

0, 1, 3, 5, 7, 10, 13, 16, 19, 22, 26, 30, 34, ... (sequenceA001768 in theOEIS)

Relation to other comparison sorts

[edit]

The algorithm is called merge-insertion sort because the initial comparisons that it performs before its recursive call (pairing up arbitrary items and comparing each pair) are the same as the initial comparisons ofmerge sort,while the comparisons that it performs after the recursive call (using binary search to insert elements one by one into a sorted list) follow the same principle asinsertion sort. In this sense, it is ahybrid algorithm that combines both merge sort and insertion sort.[9]

For small inputs (up ton=11{\displaystyle n=11}) its numbers of comparisons equal thelower bound on comparison sorting oflog2n!nlog2n1.443n{\displaystyle \lceil \log _{2}n!\rceil \approx n\log _{2}n-1.443n}. 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 thesorting numbers, which count the comparisons made by binary insertion sort or merge sort in the worst case. The sorting numbers fluctuate betweennlog2n0.915n{\displaystyle n\log _{2}n-0.915n} andnlog2nn{\displaystyle n\log _{2}n-n}, with the same leading term but a worse constant factor in the lower-order linear term.[1]

Merge-insertion sort is the sorting algorithm with the minimum possible comparisons forn{\displaystyle n} items whenevern22{\displaystyle n\leq 22}, and it has the fewest comparisons known forn46{\displaystyle n\leq 46}.[10][11]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.[3][5]It remains unknown exactly how many comparisons are needed for sorting, for alln{\displaystyle n}, but Manacher's algorithmand later record-breaking sorting algorithms have all used modifications of the merge-insertion sort ideas.[3]

References

[edit]
  1. ^abcdefgFord, Lester R. Jr.;Johnson, Selmer M. (1959), "A tournament problem",American Mathematical Monthly,66 (5):387–389,doi:10.2307/2308750,JSTOR 2308750,MR 0103159
  2. ^abcWilliamson, Stanley Gill (2002),"2.31 Merge insertion (Ford–Johnson)",Combinatorics for Computer Science, Dover books on mathematics, Courier Corporation, pp. 66–68,ISBN 9780486420769
  3. ^abcdefMahmoud, Hosam M. (2011),"12.3.1 The Ford–Johnson algorithm",Sorting: A Distribution Theory, Wiley Series in Discrete Mathematics and Optimization, vol. 54, John Wiley & Sons, pp. 286–288,ISBN 9781118031131
  4. ^abcdKnuth, Donald E. (1998), "Merge insertion",The Art of Computer Programming, Vol. 3: Sorting and Searching (2nd ed.), pp. 184–186
  5. ^abManacher, Glenn K. (July 1979), "The Ford-Johnson Sorting Algorithm Is Not Optimal",Journal of the ACM,26 (3):441–456,doi:10.1145/322139.322145
  6. ^The original description byFord & Johnson (1959) sorted the elements in descending order. The steps listed here reverse the output, following the description inKnuth (1998). The resulting algorithm makes the same comparisons but produces ascending order instead.
  7. ^Knuth (1998) credits the summation formula to the 1960 Ph.D. thesis of A. Hadian. The approximation formula was already given byFord & Johnson (1959).
  8. ^Guy, Richard K.; Nowakowski, Richard J. (December 1995), "Monthly Unsolved Problems, 1969-1995",American Mathematical Monthly,102 (10):921–926,doi:10.2307/2975272,JSTOR 2975272
  9. ^Knuth (1998), p. 184: "Since it involves some aspects of merging and some aspects of insertion, we call itmerge insertion."
  10. ^Peczarski, Marcin (2004), "New results in minimum-comparison sorting",Algorithmica,40 (2):133–145,doi:10.1007/s00453-004-1100-7,MR 2072769
  11. ^Peczarski, Marcin (2007), "The Ford-Johnson algorithm still unbeaten for less than 47 elements",Information Processing Letters,101 (3):126–128,doi:10.1016/j.ipl.2006.09.001,MR 2287331
Theory
Exchange sorts
Selection sorts
Insertion sorts
Merge sorts
Distribution sorts
Concurrent sorts
Hybrid sorts
Other
Impractical sorts
Retrieved from "https://en.wikipedia.org/w/index.php?title=Merge-insertion_sort&oldid=1335863756"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp