Knowledge

Insertion sort

Source 📝

976:, which allows elements to be spliced into or out of the list in constant time when the position in the list is known. However, searching a linked list requires sequentially following the links to the desired position: a linked list does not have random access, so it cannot use a faster method such as binary search. Therefore, the running time required for searching is O( 727:
The simplest worst case input is an array sorted in reverse order. The set of all worst case inputs consists of all arrays where each element is the smallest or second-smallest of the elements before it. In these cases every iteration of the inner loop will scan and shift the entire sorted subsection
489:
designed to insert a value into a sorted sequence at the beginning of an array. It operates by beginning at the end of the sequence and shifting each element one place to the right until a suitable position is found for the new element. The function has the side effect of overwriting the value stored
883:
outperform insertion sort for larger arrays, non-recursive sorting algorithms such as insertion sort or selection sort are generally faster for very small arrays (the exact size varies by environment and implementation, but is typically between 7 and 50 elements). Therefore, a useful optimization in
1069:
If the items are stored in a linked list, then the list can be sorted with O(1) additional space. The algorithm starts with an initially empty (and therefore trivially sorted) list. The input items are taken off the list one at a time, and then inserted in the proper place in the sorted list. When
438:
Sorting is typically done in-place, by iterating up the array, growing the sorted list behind it. At each array-position, it checks the value there against the largest value in the sorted list (which happens to be next to it, in the previous array-position checked). If larger, it leaves the element
949:
The number of swaps can be reduced by calculating the position of multiple elements before moving them. For example, if the target position of two elements is calculated before they are moved into the proper position, the number of swaps can be reduced by about 25% for random data. In the extreme
843:
In the worst case for insertion sort (when the input array is reverse-sorted), insertion sort performs just as many comparisons as selection sort. However, a disadvantage of insertion sort over selection sort is that it requires more writes due to the fact that, on each iteration, inserting the
748:
Example: The following table shows the steps for sorting the sequence {3, 7, 4, 9, 5, 2, 6, 1}. In each step, the key under consideration is underlined. The key that was moved (or left in place because it was the biggest yet considered) in the previous step is marked with an asterisk.
435:, consuming one input element each repetition, and grows a sorted output list. At each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain. 815:
The primary advantage of insertion sort over selection sort is that selection sort must always scan all remaining elements to find the absolute smallest element in the unsorted portion of the list, while insertion sort requires only a single comparison when the
426:
A graphical example of insertion sort. The partial sorted list (black) initially contains only the first element in the list. With each iteration one element (red) is removed from the "not yet checked for order" input data and inserted in-place into the sorted
497:
to insert each element encountered into its correct position. The ordered sequence into which the element is inserted is stored at the beginning of the array in the set of indices already examined. Each insertion overwrites a single value: the value being
803:
elements are in sorted order. However, the fundamental difference between the two algorithms is that insertion sort scans backwards from the current key, while selection sort scans forwards. This results in selection sort making the first k elements the
1021:
that leaves a small number of unused spaces (i.e., "gaps") spread throughout the array. The benefit is that insertions need only shift elements over until a gap is reached. The authors show that this sorting algorithm runs with high probability in
852:-st element into the sorted portion of the array requires many element swaps to shift all of the following elements, while only a single swap is required for each iteration of selection sort. In general, insertion sort will write to the array O( 744:
implementations use insertion sort for arrays smaller than a certain threshold, also when arising as subproblems; the exact threshold must be determined experimentally and depends on the machine, but is commonly around ten.
455: 465: 828:-th element; when this is frequently true (such as if the input array is already sorted or partly sorted), insertion sort is distinctly more efficient compared to selection sort. On average (assuming the rank of the 450:+ 1 entries are sorted ("+1" because the first entry is skipped). In each iteration the first remaining entry of the input is removed, and inserted into the result at the correct position, thus extending the result: 910:
If the cost of comparisons exceeds the cost of swaps, as is the case for example with string keys stored by reference or with human interaction (such as choosing one of a pair displayed side-by-side), then using
735:
The average case is also quadratic, which makes insertion sort impractical for sorting large arrays. However, insertion sort is one of the fastest algorithms for sorting very small arrays, even faster than
899:. The sorting algorithm compares elements separated by a distance that decreases on each pass. Shell sort has distinctly improved running times in practical work, with two simple variants requiring O( 423: 439:
in place and moves to the next. If smaller, it finds the correct position within the sorted list, shifts all the larger values up to make a space, and inserts into that correct position.
27: 1536:
The algorithm below uses a trailing pointer for the insertion into the sorted list. A simpler recursive method rebuilds the list each time (rather than splicing) and can use O(
216: 98: 286: 257: 168: 139: 637:
The algorithm can also be implemented in a recursive way. The recursion just replaces the outer loop, calling itself and storing successively smaller values of
724:)). During each iteration, the first remaining element of the input is only compared with the right-most element of the sorted subsection of the array. 860:) times. For this reason selection sort may be preferable in cases where writing to memory is significantly more expensive than reading, such as with 1049:, and swaps are not needed because the skip list is implemented on a linked list structure. The final running time for insertion would be 688:
It does not make the code any shorter, it also does not reduce the execution time, but it increases the additional memory consumption from
884:
the implementation of those algorithms is a hybrid approach, using the simpler algorithm when the array has been divided to a small size.
2330: 2122: 2008: 1992: 1968: 645:
equals 0, where the function then returns up the call chain to execute the code after each recursive call starting with
2342: 720:
The best case input is an array that is already sorted. In this case insertion sort has a linear running time (i.e., O(
2410: 2314: 2166: 2050: 334: 2433: 2305: 454: 840:
elements, meaning that insertion sort will perform about half as many comparisons as selection sort on average.
2741: 2268: 996:) is used, the time required for searching and insertion can be reduced significantly; this is the essence of 969:. It combines the speed of insertion sort on small data sets with the speed of merge sort on large data sets. 464: 728:
of the array before inserting the next element. This gives insertion sort a quadratic running time (i.e., O(
872: 543: 223: 175: 105: 57: 653:
increasing by 1 as each instance of the function returns to the prior instance. The initial call would be
2774: 2040: 2572: 2874: 2448: 836:-st element rank is random), insertion sort will require comparing and shifting half of the previous 2746: 930:
comparisons in the worst case. When each element in the array is searched for and inserted this is
565: 338: 538:
The outer loop runs over all the elements except the first one, because the single-element prefix
481:
The most common variant of insertion sort, which operates on arrays, can be described as follows:
916: 2654: 2534: 2488: 2458: 2182: 1956: 185: 67: 2884: 2879: 2815: 2403: 989: 972:
To avoid having to make a series of swaps for each insertion, the input could be stored in a
2794: 2659: 2514: 2245: 2196: 2028: 1008: 808:
smallest elements of the unsorted input, while in insertion sort they are simply the first
506: 342: 50: 262: 233: 144: 115: 8: 2810: 2549: 2309:, vol. 3. Sorting and Searching (second ed.), Addison-Wesley, pp. 80–105, 2700: 2675: 2649: 2453: 2378: 2249: 2223: 2104: 1997: 397: 2519: 2419: 2310: 2162: 2138: 2046: 2004: 1974: 1964: 493:
To perform an insertion sort, begin at the left-most element of the array and invoke
306: 40: 2587: 2253: 2108: 2761: 2628: 2396: 2233: 2134: 2094: 2024: 1001: 605:
to its position in one go and only performs one assignment in the inner loop body:
403: 226: 2822: 2705: 2577: 2478: 2473: 2463: 2334: 2241: 2068:"Why is insertion sort Θ(n^2) in the average case? (answer by "templatetypedef")" 919:
to determine the correct location to insert new elements, and therefore performs
569: 411: 373: 317:. It is much less efficient on large lists than more advanced algorithms such as 314: 178: 108: 60: 2385:– implementations of insertion sort in C and several other programming languages 2344:
Binary Insertion Sort – Scoreboard – Complete Investigation and C Implementation
2827: 2736: 2557: 2483: 2438: 2369: 2036: 985: 895:
made substantial improvements to the algorithm; the modified version is called
792: 601:
is a temporary variable), a slightly faster version can be produced that moves
422: 377: 360: 359:
More efficient in practice than most other simple quadratic algorithms such as
349: 2363: 2237: 1978: 667:
n > 0 insertionSortR(A, n-1) x ← A j ← n-1
2868: 2695: 2468: 2348: 369: 2710: 2623: 2493: 2300: 1013: 892: 865: 372:, i.e., efficient for data sets that are already substantially sorted: the 310: 2099: 2082: 2843: 2685: 2509: 2443: 993: 973: 946:) on average because of the series of swaps required for each insertion. 391: 364: 550:
entries are sorted is true from the start. The inner loop moves element
348:
Efficient for (quite) small data sets, much like other quadratic (i.e.,
2789: 2769: 2751: 2715: 2644: 2582: 2567: 2529: 2067: 2032: 1330:// insert current element into proper position in non-empty sorted list 966: 951: 502: 400:; i.e., only requires a constant amount O(1) of additional memory space 326: 394:; i.e., does not change the relative order of elements with equal keys 2784: 2720: 2690: 2680: 2618: 2613: 2608: 2539: 2524: 2388: 1038: 997: 896: 880: 876: 741: 737: 432: 318: 2274: 630:
The new inner loop shifts elements to the right to clear a spot for
2853: 2848: 2562: 2228: 322: 2779: 1070:
the input list is empty, the sorted list has the desired result.
26: 2365:
Insertion Sort – a comparison with other O(n) sorting algorithms
1011:, and Mosteiro published a new variant of insertion sort called 1438:// insert into middle of the sorted list or as the last element 861: 1963:(2nd ed.). ACM Press / Addison-Wesley. pp. 115–116. 965:
to sort groups of 32 elements, followed by a final sort using
915:
may yield better performance. Binary insertion sort employs a
2194: 2045:(3rd ed.), MIT Press and McGraw-Hill, pp. 16–18, 414:
hand, most use a method that is similar to insertion sort.
2023: 1680:/* take items off the input list one by one until empty */ 942:. The algorithm as a whole still has a running time of O( 505:
of the complete algorithm follows, where the arrays are
786: 675:A > x A ← A j ← j-1 554:
to its correct place so that after the loop, the first
329:. However, insertion sort provides several advantages: 2337: (archived 8 March 2015) – graphical demonstration 696:(at the deepest level of recursion the stack contains 1156:// head is the first element of resulting sorted list 265: 236: 188: 147: 118: 70: 1288:// or as the first element into an empty sorted list 1656:/* build up the sorted array from the empty list */ 490:
immediately after the sorted sequence in the array.
1996: 1776:/* splice head into sorted list at proper place */ 856:) times, whereas selection sort will write only O( 280: 251: 210: 162: 133: 92: 2199:; Mosteiro, Miguel A. (2006). "Insertion sort is 715: 384:) when each element in the input is no more than 2866: 2267:Hill, Curt (ed.), "Trailing Pointer Technique", 1064: 704:array, each with accompanying value of variable 1041:is used, the insertion time is brought down to 2273:, Valley City State University, archived from 474:copied to the right as it is compared against 2404: 620:A > x A ← A j ← j - 1 2080: 446:iterations has the property where the first 2331:Animated Sorting Algorithms: Insertion Sort 2125:(1986). "A New Upper Bound for Shellsort". 1728:/* trailing pointer for efficient splice */ 2411: 2397: 1285:// insert into the head of the sorted list 2227: 2121: 2115: 2098: 1991: 612:i < length(A) x ← A j ← i 406:; i.e., can sort a list as it receives it 2039:(2009) , "Section 2.1: Insertion sort", 1950: 1948: 1946: 1944: 568:, otherwise the test might result in an 421: 2303:(1998), "5.2.1: Sorting by Insertion", 2156: 1954: 485:Suppose there exists a function called 341:-like pseudo-code, and five lines when 337:shows a version that is three lines in 2867: 2418: 2392: 2340: 2299: 2081:Frank, R. M.; Lazarus, R. B. (1960). 1941: 410:When people manually sort cards in a 2152: 2150: 2148: 950:case, this variant works similar to 799:passes through the array, the first 787:Relation to other sorting algorithms 388:places away from its sorted position 2065: 663:insertionSortR(array A, int n) 558:elements are sorted. Note that the 13: 2293: 1399:// last element of the sorted list 791:Insertion sort is very similar to 14: 2896: 2324: 2145: 1845:/* no - continue down the list */ 980:), and the time for sorting is O( 2266: 2083:"A High-Speed Sorting Procedure" 824:-st element is greater than the 463: 453: 25: 2434:Computational complexity theory 2306:The Art of Computer Programming 2260: 705: 701: 564:-operator in the test must use 516:i < length(A) j ← i 470:with each element greater than 2188: 2175: 2074: 2059: 2017: 2003:. Addison-Wesley. p. 95. 1985: 1614:// zero or one element in list 1108:// zero or one element in list 795:. As in selection sort, after 716:Best, worst, and average cases 655:insertionSortR(A, length(A)-1) 528:A and A j ← j - 1 275: 269: 246: 240: 205: 192: 157: 151: 128: 122: 87: 74: 1: 2161:. PHI Learning. p. 549. 1934: 1065:List insertion sort code in C 873:divide-and-conquer algorithms 313:(or list) one item at a time 2139:10.1016/0196-6774(86)90001-5 1842:/* does head belong here? */ 542:is trivially sorted, so the 417: 7: 2216:Theory of Computing Systems 984:). If a more sophisticated 887: 10: 2903: 2742:Batcher odd–even mergesort 2042:Introduction to Algorithms 442:The resulting array after 2836: 2803: 2760: 2729: 2668: 2637: 2596: 2548: 2502: 2426: 2347:, Pathcom, archived from 2238:10.1007/s00224-005-1237-z 2157:Samanta, Debasis (2008). 2087:Communications of the ACM 576:and it tries to evaluate 292: 222: 174: 104: 56: 46: 36: 24: 2747:Pairwise sorting network 2383:(wiki), LiteratePrograms 1542: 1072: 566:short-circuit evaluation 211:{\displaystyle O(n^{2})} 93:{\displaystyle O(n^{2})} 31:Insertion sort animation 2775:Kirkpatrick–Reisch sort 2159:Classic Data Structures 1755:/* pop head off list */ 1704:/* remember the head */ 812:elements of the input. 783:1* 2 3 4 5 6 7 9 779:1 2 3 4 5 6* 7 9 775:6 1 2* 3 4 5 7 9 771:2 6 1 3 4 5* 7 9 767:5 2 6 1 3 4 7 9* 763:9 5 2 6 1 3 4* 7 759:4 9 5 2 6 1 3 7* 755:7 4 9 5 2 6 1 3* 333:Simple implementation: 2655:Oscillating merge sort 2535:Proportion extend sort 2489:Transdichotomous model 2341:Adamovsky, John Paul, 593:operation in-place as 428: 309:that builds the final 282: 253: 212: 164: 135: 94: 2816:Pre-topological order 2197:Farach-Colton, Martín 2127:Journal of Algorithms 2100:10.1145/366947.366957 2029:Leiserson, Charles E. 1955:Bentley, Jon (2000). 1432:// middle of the list 1019:gapped insertion sort 963:binary insertion sort 913:binary insertion sort 425: 356:)) sorting algorithms 283: 254: 218:comparisons and swaps 213: 165: 136: 100:comparisons and swaps 95: 2795:Merge-insertion sort 2660:Polyphase merge sort 2515:Cocktail shaker sort 2195:Bender, Michael A.; 1957:"Column 11: Sorting" 1009:Martin Farach-Colton 624:A ← x i ← i + 1 587:After expanding the 281:{\displaystyle O(1)} 263: 252:{\displaystyle O(n)} 234: 186: 163:{\displaystyle O(1)} 145: 134:{\displaystyle O(n)} 116: 68: 2811:Topological sorting 2573:Cartesian tree sort 2183:"Binary Merge Sort" 641:on the stack until 595:x ← A; A ← A; A ← x 21: 2701:Interpolation sort 2676:American flag sort 2669:Distribution sorts 2650:Cascade merge sort 2420:Sorting algorithms 2380:Insertion sort (C) 1961:Programming Pearls 700:references to the 570:array bounds error 429: 278: 249: 208: 160: 131: 90: 19: 2862: 2861: 2837:Impractical sorts 2123:Sedgewick, Robert 2070:. Stack Overflow. 2033:Rivest, Ronald L. 2025:Cormen, Thomas H. 2010:978-0-201-06672-2 1993:Sedgewick, Robert 1970:978-0-201-65788-3 959:binary merge sort 649:equal to 1, with 524:A > A 307:sorting algorithm 300: 299: 41:Sorting algorithm 16:Sorting algorithm 2892: 2875:Comparison sorts 2770:Block merge sort 2730:Concurrent sorts 2629:Patience sorting 2413: 2406: 2399: 2390: 2389: 2384: 2373: 2358: 2357: 2356: 2319: 2287: 2285: 2284: 2282: 2277:on 26 April 2012 2264: 2258: 2257: 2231: 2213: 2192: 2186: 2185: 2179: 2173: 2172: 2154: 2143: 2142: 2119: 2113: 2112: 2102: 2078: 2072: 2071: 2066:Schwarz, Keith. 2063: 2057: 2055: 2021: 2015: 2014: 2002: 1989: 1983: 1982: 1952: 1930: 1927: 1924: 1921: 1918: 1915: 1912: 1909: 1906: 1903: 1900: 1897: 1894: 1891: 1888: 1885: 1882: 1879: 1876: 1873: 1870: 1867: 1864: 1861: 1858: 1855: 1852: 1849: 1846: 1843: 1840: 1837: 1834: 1831: 1828: 1825: 1822: 1819: 1816: 1813: 1810: 1807: 1804: 1801: 1798: 1795: 1792: 1789: 1786: 1783: 1780: 1777: 1774: 1771: 1768: 1765: 1762: 1759: 1756: 1753: 1750: 1747: 1744: 1741: 1738: 1735: 1732: 1729: 1726: 1723: 1720: 1717: 1714: 1711: 1708: 1705: 1702: 1699: 1696: 1693: 1690: 1687: 1684: 1681: 1678: 1675: 1672: 1669: 1666: 1663: 1660: 1657: 1654: 1651: 1648: 1645: 1642: 1639: 1636: 1633: 1630: 1627: 1624: 1621: 1618: 1615: 1612: 1609: 1606: 1603: 1600: 1597: 1594: 1591: 1588: 1585: 1582: 1579: 1576: 1573: 1570: 1567: 1564: 1561: 1558: 1555: 1552: 1549: 1546: 1532: 1529: 1526: 1523: 1520: 1517: 1514: 1511: 1508: 1505: 1502: 1499: 1496: 1493: 1490: 1487: 1484: 1481: 1478: 1475: 1472: 1469: 1466: 1463: 1460: 1457: 1454: 1451: 1448: 1445: 1442: 1439: 1436: 1433: 1430: 1427: 1424: 1421: 1418: 1415: 1412: 1409: 1406: 1403: 1400: 1397: 1394: 1391: 1388: 1385: 1382: 1379: 1376: 1373: 1370: 1367: 1364: 1361: 1358: 1355: 1352: 1349: 1346: 1343: 1340: 1337: 1334: 1331: 1328: 1325: 1322: 1319: 1316: 1313: 1310: 1307: 1304: 1301: 1298: 1295: 1292: 1289: 1286: 1283: 1280: 1277: 1274: 1271: 1268: 1265: 1262: 1259: 1256: 1253: 1250: 1247: 1244: 1241: 1238: 1235: 1232: 1229: 1226: 1223: 1220: 1217: 1214: 1211: 1208: 1205: 1202: 1199: 1196: 1193: 1190: 1187: 1184: 1181: 1178: 1175: 1172: 1169: 1166: 1163: 1160: 1157: 1154: 1151: 1148: 1145: 1142: 1139: 1136: 1133: 1130: 1127: 1124: 1121: 1118: 1115: 1112: 1109: 1106: 1103: 1100: 1097: 1094: 1091: 1088: 1085: 1082: 1079: 1076: 1060: 1048: 1033: 1007:In 2006 Bender, 1002:binary tree sort 957:A variant named 941: 929: 907:) running time. 859: 851: 835: 823: 711: 707: 703: 699: 695: 691: 633: 604: 600: 596: 592: 583: 580:(i.e. accessing 579: 575: 563: 557: 553: 549: 541: 467: 457: 387: 287: 285: 284: 279: 258: 256: 255: 250: 227:space complexity 217: 215: 214: 209: 204: 203: 169: 167: 166: 161: 140: 138: 137: 132: 99: 97: 96: 91: 86: 85: 29: 22: 18: 2902: 2901: 2895: 2894: 2893: 2891: 2890: 2889: 2865: 2864: 2863: 2858: 2832: 2823:Pancake sorting 2799: 2756: 2725: 2706:Pigeonhole sort 2664: 2633: 2597:Insertion sorts 2592: 2578:Tournament sort 2550:Selection sorts 2544: 2498: 2479:Integer sorting 2474:Sorting network 2464:Comparison sort 2422: 2417: 2377: 2362: 2354: 2352: 2335:Wayback Machine 2327: 2317: 2296: 2294:Further reading 2291: 2290: 2280: 2278: 2265: 2261: 2200: 2193: 2189: 2181: 2180: 2176: 2169: 2155: 2146: 2120: 2116: 2079: 2075: 2064: 2060: 2053: 2037:Stein, Clifford 2022: 2018: 2011: 1990: 1986: 1971: 1953: 1942: 1937: 1932: 1931: 1928: 1925: 1922: 1919: 1916: 1913: 1910: 1907: 1904: 1901: 1898: 1895: 1892: 1889: 1886: 1883: 1880: 1877: 1874: 1871: 1868: 1865: 1862: 1859: 1856: 1853: 1850: 1847: 1844: 1841: 1838: 1835: 1832: 1829: 1826: 1823: 1820: 1817: 1814: 1811: 1808: 1805: 1802: 1799: 1796: 1793: 1790: 1787: 1784: 1781: 1778: 1775: 1772: 1769: 1766: 1763: 1760: 1757: 1754: 1751: 1748: 1745: 1742: 1739: 1736: 1733: 1730: 1727: 1724: 1721: 1718: 1715: 1712: 1709: 1706: 1703: 1700: 1697: 1694: 1691: 1688: 1685: 1682: 1679: 1676: 1673: 1670: 1667: 1664: 1661: 1658: 1655: 1652: 1649: 1646: 1643: 1640: 1637: 1634: 1631: 1628: 1625: 1622: 1619: 1616: 1613: 1610: 1607: 1604: 1601: 1598: 1595: 1592: 1589: 1586: 1583: 1580: 1577: 1574: 1571: 1568: 1565: 1562: 1559: 1556: 1553: 1550: 1547: 1544: 1540:) stack space. 1534: 1533: 1530: 1527: 1524: 1521: 1518: 1515: 1512: 1509: 1506: 1503: 1500: 1497: 1494: 1491: 1488: 1485: 1482: 1479: 1476: 1473: 1470: 1467: 1464: 1461: 1458: 1455: 1452: 1449: 1446: 1443: 1440: 1437: 1434: 1431: 1428: 1425: 1422: 1419: 1416: 1413: 1410: 1407: 1404: 1401: 1398: 1395: 1392: 1389: 1386: 1383: 1380: 1377: 1374: 1371: 1368: 1365: 1362: 1359: 1356: 1353: 1350: 1347: 1344: 1341: 1338: 1335: 1332: 1329: 1326: 1323: 1320: 1317: 1314: 1311: 1308: 1305: 1302: 1299: 1296: 1293: 1290: 1287: 1284: 1281: 1278: 1275: 1272: 1269: 1266: 1263: 1260: 1257: 1254: 1251: 1248: 1245: 1242: 1239: 1236: 1233: 1230: 1227: 1224: 1221: 1218: 1215: 1212: 1209: 1206: 1203: 1200: 1197: 1194: 1191: 1188: 1185: 1182: 1179: 1176: 1173: 1170: 1167: 1164: 1161: 1158: 1155: 1152: 1149: 1146: 1143: 1140: 1137: 1134: 1131: 1128: 1125: 1122: 1119: 1116: 1113: 1110: 1107: 1104: 1101: 1098: 1095: 1092: 1089: 1086: 1083: 1080: 1077: 1074: 1067: 1050: 1042: 1023: 931: 924: 920: 890: 857: 845: 829: 817: 789: 784: 740:; indeed, good 718: 709: 697: 693: 689: 686: 631: 628: 602: 598: 594: 588: 581: 577: 573: 559: 555: 551: 547: 546:that the first 539: 536: 431:Insertion sort 420: 385: 374:time complexity 264: 261: 260: 235: 232: 231: 199: 195: 187: 184: 183: 146: 143: 142: 117: 114: 113: 81: 77: 69: 66: 65: 32: 17: 12: 11: 5: 2900: 2899: 2888: 2887: 2882: 2877: 2860: 2859: 2857: 2856: 2851: 2846: 2840: 2838: 2834: 2833: 2831: 2830: 2828:Spaghetti sort 2825: 2820: 2819: 2818: 2807: 2805: 2801: 2800: 2798: 2797: 2792: 2787: 2782: 2777: 2772: 2766: 2764: 2758: 2757: 2755: 2754: 2749: 2744: 2739: 2737:Bitonic sorter 2733: 2731: 2727: 2726: 2724: 2723: 2718: 2713: 2708: 2703: 2698: 2693: 2688: 2683: 2678: 2672: 2670: 2666: 2665: 2663: 2662: 2657: 2652: 2647: 2641: 2639: 2635: 2634: 2632: 2631: 2626: 2621: 2616: 2611: 2606: 2604:Insertion sort 2600: 2598: 2594: 2593: 2591: 2590: 2588:Weak-heap sort 2585: 2580: 2575: 2570: 2565: 2560: 2558:Selection sort 2554: 2552: 2546: 2545: 2543: 2542: 2537: 2532: 2527: 2522: 2517: 2512: 2506: 2504: 2503:Exchange sorts 2500: 2499: 2497: 2496: 2491: 2486: 2481: 2476: 2471: 2466: 2461: 2456: 2451: 2446: 2441: 2439:Big O notation 2436: 2430: 2428: 2424: 2423: 2416: 2415: 2408: 2401: 2393: 2387: 2386: 2375: 2360: 2338: 2326: 2325:External links 2323: 2322: 2321: 2315: 2295: 2292: 2289: 2288: 2259: 2222:(3): 391–397. 2187: 2174: 2167: 2144: 2133:(2): 159–173. 2114: 2073: 2058: 2056:. See page 18. 2051: 2016: 2009: 1984: 1969: 1939: 1938: 1936: 1933: 1543: 1073: 1066: 1063: 986:data structure 922: 889: 886: 793:selection sort 788: 785: 751: 717: 714: 659: 607: 511: 500: 499: 491: 419: 416: 408: 407: 401: 395: 389: 367: 361:selection sort 357: 346: 315:by comparisons 303:Insertion sort 298: 297: 294: 290: 289: 277: 274: 271: 268: 248: 245: 242: 239: 229: 220: 219: 207: 202: 198: 194: 191: 181: 172: 171: 159: 156: 153: 150: 130: 127: 124: 121: 111: 102: 101: 89: 84: 80: 76: 73: 63: 54: 53: 48: 47:Data structure 44: 43: 38: 34: 33: 30: 20:Insertion sort 15: 9: 6: 4: 3: 2: 2898: 2897: 2886: 2883: 2881: 2878: 2876: 2873: 2872: 2870: 2855: 2852: 2850: 2847: 2845: 2842: 2841: 2839: 2835: 2829: 2826: 2824: 2821: 2817: 2814: 2813: 2812: 2809: 2808: 2806: 2802: 2796: 2793: 2791: 2788: 2786: 2783: 2781: 2778: 2776: 2773: 2771: 2768: 2767: 2765: 2763: 2759: 2753: 2750: 2748: 2745: 2743: 2740: 2738: 2735: 2734: 2732: 2728: 2722: 2719: 2717: 2714: 2712: 2709: 2707: 2704: 2702: 2699: 2697: 2696:Counting sort 2694: 2692: 2689: 2687: 2684: 2682: 2679: 2677: 2674: 2673: 2671: 2667: 2661: 2658: 2656: 2653: 2651: 2648: 2646: 2643: 2642: 2640: 2636: 2630: 2627: 2625: 2622: 2620: 2617: 2615: 2612: 2610: 2607: 2605: 2602: 2601: 2599: 2595: 2589: 2586: 2584: 2581: 2579: 2576: 2574: 2571: 2569: 2566: 2564: 2561: 2559: 2556: 2555: 2553: 2551: 2547: 2541: 2538: 2536: 2533: 2531: 2528: 2526: 2523: 2521: 2520:Odd–even sort 2518: 2516: 2513: 2511: 2508: 2507: 2505: 2501: 2495: 2492: 2490: 2487: 2485: 2484:X + Y sorting 2482: 2480: 2477: 2475: 2472: 2470: 2469:Adaptive sort 2467: 2465: 2462: 2460: 2457: 2455: 2452: 2450: 2447: 2445: 2442: 2440: 2437: 2435: 2432: 2431: 2429: 2425: 2421: 2414: 2409: 2407: 2402: 2400: 2395: 2394: 2391: 2382: 2381: 2376: 2371: 2367: 2366: 2361: 2351:on 2012-02-24 2350: 2346: 2345: 2339: 2336: 2332: 2329: 2328: 2318: 2316:0-201-89685-0 2312: 2308: 2307: 2302: 2301:Knuth, Donald 2298: 2297: 2276: 2272: 2271: 2263: 2255: 2251: 2247: 2243: 2239: 2235: 2230: 2225: 2221: 2217: 2211: 2207: 2203: 2198: 2191: 2184: 2178: 2170: 2168:9788120337312 2164: 2160: 2153: 2151: 2149: 2140: 2136: 2132: 2128: 2124: 2118: 2110: 2106: 2101: 2096: 2092: 2088: 2084: 2077: 2069: 2062: 2054: 2052:0-262-03384-4 2048: 2044: 2043: 2038: 2034: 2030: 2026: 2020: 2012: 2006: 2001: 2000: 1994: 1988: 1980: 1976: 1972: 1966: 1962: 1958: 1951: 1949: 1947: 1945: 1940: 1541: 1539: 1071: 1062: 1058: 1054: 1046: 1040: 1035: 1031: 1027: 1020: 1016: 1015: 1010: 1005: 1003: 999: 995: 991: 987: 983: 979: 975: 970: 968: 964: 960: 955: 953: 947: 945: 939: 935: 927: 918: 917:binary search 914: 908: 906: 902: 898: 894: 885: 882: 878: 874: 869: 867: 863: 855: 849: 841: 839: 833: 827: 821: 813: 811: 807: 802: 798: 794: 782: 778: 774: 770: 766: 762: 758: 754: 750: 746: 743: 739: 733: 731: 725: 723: 713: 685: 682: 678: 674: 670: 666: 662: 658: 656: 652: 648: 644: 640: 635: 627: 623: 619: 615: 611: 606: 591: 585: 571: 567: 562: 545: 535: 531: 527: 523: 519: 515: 510: 508: 504: 496: 492: 488: 484: 483: 482: 479: 477: 473: 468: 466: 461: 458: 456: 451: 449: 445: 440: 436: 434: 424: 415: 413: 405: 402: 399: 396: 393: 390: 383: 379: 375: 371: 368: 366: 362: 358: 355: 351: 347: 344: 340: 336: 332: 331: 330: 328: 324: 320: 316: 312: 308: 304: 295: 291: 272: 266: 243: 237: 230: 228: 225: 221: 200: 196: 189: 182: 180: 177: 173: 154: 148: 141:comparisons, 125: 119: 112: 110: 107: 103: 82: 78: 71: 64: 62: 59: 55: 52: 49: 45: 42: 39: 35: 28: 23: 2885:Online sorts 2880:Stable sorts 2762:Hybrid sorts 2711:Proxmap sort 2624:Library sort 2603: 2494:Quantum sort 2379: 2364: 2353:, retrieved 2349:the original 2343: 2304: 2281:22 September 2279:, retrieved 2275:the original 2269: 2262: 2219: 2215: 2209: 2205: 2201: 2190: 2177: 2158: 2130: 2126: 2117: 2093:(1): 20–22. 2090: 2086: 2076: 2061: 2041: 2019: 1998: 1987: 1960: 1537: 1535: 1068: 1056: 1052: 1044: 1036: 1029: 1025: 1018: 1014:library sort 1012: 1006: 981: 977: 971: 962: 958: 956: 948: 943: 937: 933: 925: 912: 909: 904: 900: 891: 870: 866:flash memory 853: 847: 842: 837: 831: 825: 819: 814: 809: 805: 800: 796: 790: 780: 776: 772: 768: 764: 760: 756: 752: 747: 734: 729: 726: 721: 719: 712:down to 1). 687: 684:end function 683: 680: 676: 672: 668: 664: 660: 654: 650: 646: 642: 638: 636: 629: 625: 621: 617: 613: 609: 589: 586: 560: 537: 533: 529: 525: 521: 517: 513: 501: 494: 486: 480: 475: 471: 469: 462: 459: 452: 447: 443: 441: 437: 430: 409: 381: 353: 311:sorted array 305:is a simple 302: 301: 2844:Stooge sort 2686:Bucket sort 2638:Merge sorts 2510:Bubble sort 2454:Inplacement 2444:Total order 994:binary tree 974:linked list 871:While some 365:bubble sort 335:Jon Bentley 179:performance 109:performance 61:performance 2869:Categories 2790:Spreadsort 2752:Samplesort 2716:Radix sort 2645:Merge sort 2583:Cycle sort 2568:Smoothsort 2530:Gnome sort 2372:: Core war 2355:2009-10-21 2229:cs/0407003 1999:Algorithms 1979:1047840657 1935:References 967:merge sort 952:merge sort 897:Shell sort 893:D.L. Shell 679:A ← x 671:j >= 0 532:i ← i + 1 507:zero-based 503:Pseudocode 327:merge sort 224:Worst-case 58:Worst-case 2785:Introsort 2721:Flashsort 2691:Burstsort 2681:Bead sort 2619:Tree sort 2614:Splaysort 2609:Shellsort 2540:Quicksort 2525:Comb sort 2459:Stability 1084:SortList1 1039:skip list 998:heap sort 881:mergesort 877:quicksort 742:quicksort 738:quicksort 677:end while 626:end while 622:end while 616:j > 0 544:invariant 534:end while 530:end while 520:j > 0 498:inserted. 418:Algorithm 343:optimized 319:quicksort 288:auxiliary 106:Best-case 2854:Bogosort 2849:Slowsort 2563:Heapsort 2254:14701669 2109:34066017 1995:(1983). 1590:SortList 903:) and O( 888:Variants 875:such as 661:function 584:fails). 578:A > A 460:becomes 433:iterates 398:In-place 370:Adaptive 323:heapsort 2780:Timsort 2333:at the 2246:2218409 1923:pSorted 1905:ppTrail 1896:ppTrail 1863:ppTrail 1848:ppTrail 1824:ppTrail 1794:ppTrail 1749:pSorted 1740:ppTrail 1668:pSorted 1489:// done 1477:current 1441:current 1402:current 1315:current 1291:current 1258:current 1210:current 988:(e.g., 961:uses a 597:(where 572:, when 293:Optimal 259:total, 176:Average 2427:Theory 2313:  2252:  2244:  2165:  2107:  2049:  2007:  1977:  1967:  1920:return 1833:iValue 1812:iValue 1731:struct 1707:struct 1659:struct 1647:return 1596:struct 1581:struct 1572:iValue 1554:struct 1545:struct 1522:return 1426:iValue 1408:iValue 1333:struct 1276:iValue 1264:iValue 1201:struct 1159:struct 1147:return 1090:struct 1075:struct 1043:O(log 1034:time. 862:EEPROM 681:end if 608:i ← 1 512:i ← 1 495:Insert 487:Insert 412:bridge 404:Online 392:Stable 2804:Other 2449:Lists 2270:Euler 2250:S2CID 2224:arXiv 2105:S2CID 1911:pHead 1887:pNext 1884:-> 1881:pHead 1872:pNext 1869:-> 1854:& 1830:-> 1809:-> 1806:pHead 1779:while 1770:pNext 1767:-> 1764:pList 1758:pList 1746:& 1722:pList 1716:pHead 1689:pList 1683:while 1650:pList 1641:pNext 1638:-> 1635:pList 1626:pList 1605:pList 1563:pNext 1507:pNext 1504:-> 1483:break 1471:pNext 1468:-> 1459:pNext 1456:-> 1447:pNext 1444:-> 1423:-> 1420:pNext 1417:-> 1405:-> 1387:pNext 1384:-> 1354:while 1297:pNext 1294:-> 1273:-> 1261:-> 1234:pNext 1231:-> 1228:pList 1222:pList 1216:pList 1186:pList 1180:while 1150:pList 1135:pNext 1132:-> 1129:pList 1117:pList 1099:pList 1037:If a 708:from 669:while 632:x = A 614:while 610:while 518:while 514:while 427:list. 325:, or 170:swaps 51:Array 37:Class 2311:ISBN 2283:2012 2208:log 2163:ISBN 2047:ISBN 2005:ISBN 1975:OCLC 1965:ISBN 1815:< 1800:NULL 1734:LIST 1710:LIST 1695:NULL 1674:NULL 1662:LIST 1599:LIST 1584:LIST 1557:LIST 1548:LIST 1525:head 1411:< 1393:NULL 1366:NULL 1348:head 1336:LIST 1324:else 1309:head 1303:head 1270:head 1267:< 1252:NULL 1246:head 1204:LIST 1192:NULL 1174:NULL 1168:head 1162:LIST 1141:NULL 1123:NULL 1093:LIST 1078:LIST 1055:log 1028:log 1000:and 990:heap 936:log 921:⌈log 879:and 850:+ 1) 834:+ 1) 822:+ 1) 732:)). 694:O(N) 690:O(1) 590:swap 526:swap 2234:doi 2214:". 2135:doi 2095:doi 1569:int 1017:or 992:or 864:or 692:to 673:and 618:and 574:j=0 561:and 556:i+1 522:and 376:is 363:or 2871:: 2370:UK 2368:, 2248:. 2242:MR 2240:. 2232:. 2220:39 2218:. 2147:^ 2129:. 2103:. 2089:. 2085:. 2035:; 2031:; 2027:; 1973:. 1959:. 1943:^ 1836:)) 1803:|| 1797:== 1737:** 1692:!= 1629:|| 1617:if 1578:}; 1396:|| 1390:== 1375:if 1363:!= 1255:|| 1249:== 1240:if 1189:!= 1138:== 1126:|| 1120:== 1111:if 1061:. 1051:O( 1024:O( 1004:. 954:. 932:O( 868:. 665:if 657:. 634:. 509:: 478:. 382:kn 321:, 296:No 2412:e 2405:t 2398:v 2374:. 2359:. 2320:. 2286:. 2256:. 2236:: 2226:: 2212:) 2210:n 2206:n 2204:( 2202:O 2171:. 2141:. 2137:: 2131:7 2111:. 2097:: 2091:3 2013:. 1981:. 1929:} 1926:; 1917:} 1914:; 1908:= 1902:* 1899:; 1893:* 1890:= 1878:} 1875:; 1866:) 1860:* 1857:( 1851:= 1839:{ 1827:) 1821:* 1818:( 1791:* 1788:( 1785:! 1782:( 1773:; 1761:= 1752:; 1743:= 1725:; 1719:= 1713:* 1701:{ 1698:) 1686:( 1677:; 1671:= 1665:* 1653:; 1644:) 1632:! 1623:! 1620:( 1611:{ 1608:) 1602:* 1593:( 1587:* 1575:; 1566:; 1560:* 1551:{ 1538:n 1531:} 1528:; 1519:} 1516:} 1513:} 1510:; 1501:p 1498:= 1495:p 1492:} 1486:; 1480:; 1474:= 1465:p 1462:; 1453:p 1450:= 1435:{ 1429:) 1414:p 1381:p 1378:( 1372:{ 1369:) 1360:p 1357:( 1351:; 1345:= 1342:p 1339:* 1327:{ 1321:} 1318:; 1312:= 1306:; 1300:= 1282:{ 1279:) 1243:( 1237:; 1225:= 1219:; 1213:= 1207:* 1198:{ 1195:) 1183:( 1177:; 1171:= 1165:* 1153:; 1144:) 1114:( 1105:{ 1102:) 1096:* 1087:( 1081:* 1059:) 1057:n 1053:n 1047:) 1045:n 1032:) 1030:n 1026:n 982:n 978:n 944:n 940:) 938:n 934:n 928:⌉ 926:n 923:2 905:n 901:n 858:n 854:n 848:k 846:( 838:k 832:k 830:( 826:k 820:k 818:( 810:k 806:k 801:k 797:k 781:1 777:6 773:2 769:5 765:9 761:4 757:7 753:3 730:n 722:n 710:N 706:n 702:A 698:N 651:n 647:n 643:n 639:n 603:A 599:x 582:A 552:A 548:i 540:A 476:x 472:x 448:k 444:k 386:k 380:( 378:O 354:n 352:( 350:O 345:. 339:C 276:) 273:1 270:( 267:O 247:) 244:n 241:( 238:O 206:) 201:2 197:n 193:( 190:O 158:) 155:1 152:( 149:O 129:) 126:n 123:( 120:O 88:) 83:2 79:n 75:( 72:O

Index


Sorting algorithm
Array
Worst-case
performance
Best-case
performance
Average
performance
Worst-case
space complexity
sorting algorithm
sorted array
by comparisons
quicksort
heapsort
merge sort
Jon Bentley
C
optimized
O
selection sort
bubble sort
Adaptive
time complexity
O
Stable
In-place
Online
bridge

Text is available under the Creative Commons Attribution-ShareAlike License. Additional terms may apply.