Description
Shellsort is an optimization of insertion sort that allows the exchange of items that are far apart. The idea is to arrange the list of elements so that, starting anywhere, taking every ''h''th element produces a sorted list. Such a list is said to be ''h''-sorted. It can also be thought of as ''h'' interleaved lists, each individually sorted. Beginning with large values of ''h'' allows elements to move long distances in the original list, reducing large amounts of disorder quickly, and leaving less work for smaller ''h''-sort steps to do. If the list is then ''k-sorted'' for some smaller integer ''k'', then the list remains ''h''-sorted. A final sort with ''h'' = 1 ensures the list is fully sorted at the end, but a judiciously chosen decreasing sequence of ''h'' values leaves very little work for this final pass to do. In simplistic terms, this means if we have an array of 1024 numbers, our first gap (''h'') could be 512. We then run through the list comparing each element in the first half to the element in the second half. Our second gap (''k'') is 256, which breaks the array into four sections (starting at 0, 256, 512, 768), and we make sure the first items in each section are sorted relative to each other, then the second item in each section, and so on. In practice the gap sequence could be anything, but the last gap is always 1 to finish the sort (effectively finishing with an ordinary insertion sort). An example run of Shellsort with gaps 5, 3 and 1 is shown below. The first pass, 5-sorting, performs insertion sort on five separate subarrays (''a''1, ''a''6, ''a''11), (''a''2, ''a''7, ''a''12), (''a''3, ''a''8), (''a''4, ''a''9), (''a''5, ''a''10). For instance, it changes the subarray (''a''1, ''a''6, ''a''11) from (62, 17, 25) to (17, 25, 62). The next pass, 3-sorting, performs insertion sort on the three subarrays (''a''1, ''a''4, ''a''7, ''a''10), (''a''2, ''a''5, ''a''8, ''a''11), (''a''3, ''a''6, ''a''9, ''a''12). The last pass, 1-sorting, is an ordinary insertion sort of the entire array (''a''1,..., ''a''12). As the example illustrates, the subarrays that Shellsort operates on are initially short; later they are longer but almost ordered. In both cases insertion sort works efficiently. Unlike insertion sort, Shellsort is not a stable sort since gapped insertions transport equal elements past one another and thus lose their original order. It is an adaptive sorting algorithm in that it executes faster when the input is partially sorted.Pseudocode
Using Marcin Ciura's gap sequence, with an inner insertion sort.Gap sequences
The question of deciding which gap sequence to use is difficult. Every gap sequence that contains 1 yields a correct sort (as this makes the final pass an ordinary insertion sort); however, the properties of thus obtained versions of Shellsort may be very different. Too few gaps slows down the passes, and too many gaps produces an overhead. The table below compares most proposed gap sequences published so far. Some of them have decreasing elements that depend on the size of the sorted array (''N''). Others are increasing infinite sequences, whose elements less than ''N'' should be used in reverse order. When the binary representation of ''N'' contains many consecutive zeroes, Shellsort using Shell's original gap sequence makes Θ(''N''2) comparisons in the worst case. For instance, this case occurs for ''N'' equal to a power of two when elements greater and smaller than the median occupy odd and even positions respectively, since they are compared only in the last pass. Although it has higher complexity than the ''O''(''N'' log ''N'') that is optimal for comparison sorts, Pratt's version lends itself to sorting networks and has the same asymptotic gate complexity as Batcher's bitonic sorter. Gonnet and Baeza-Yates observed that Shellsort makes the fewest comparisons on average when the ratios of successive gaps are roughly equal to 2.2. This is why their sequence with ratio 2.2 and Tokuda's sequence with ratio 2.25 prove efficient. However, it is not known why this is so. Sedgewick recommends using gaps which have lowComputational complexity
The following property holds: after ''h''2-sorting of any ''h''1-sorted array, the array remains ''h''1-sorted. Every ''h''1-sorted and ''h''2-sorted array is also (''a''1''h''1+''a''2''h''2)-sorted, for any nonnegative integers ''a''1 and ''a''2. The worst-case complexity of Shellsort is therefore connected with the Frobenius problem: for given integers ''h''1,..., ''hn'' with gcd = 1, the Frobenius number ''g''(''h''1,..., ''hn'') is the greatest integer that cannot be represented as ''a''1''h''1+ ... +''anhn'' with nonnegative integer ''a''1,..., ''an''. Using known formulae for Frobenius numbers, we can determine the worst-case complexity of Shellsort for several classes of gap sequences. Proven results are shown in the above table. Mark Allen Weiss proved that Shellsort runs in ''O''(''N'' log ''N'') time when the input array is in reverse order. With respect to the average number of operations, none of the proven results concerns a practical gap sequence. For gaps that are powers of two, Espelid computed this average as . Knuth determined the average complexity of sorting an ''N''-element array with two gaps (''h'', 1) to be . It follows that a two-pass Shellsort with ''h'' = Θ(''N''1/3) makes on average ''O''(''N''5/3) comparisons/inversions/running time. Yao found the average complexity of a three-pass Shellsort. His result was refined by Janson and Knuth: the average number of comparisons/inversions/running time made during a Shellsort with three gaps (''ch'', ''cg'', 1), where ''h'' and ''g'' are coprime, is in the first pass, in the second pass and in the third pass. ''ψ''(''h'', ''g'') in the last formula is a complicated function asymptotically equal to . In particular, when ''h'' = Θ(''N''7/15) and ''g'' = Θ(''N''1/5), the average time of sorting is ''O''(''N''23/15). Based on experiments, it is conjectured that Shellsort with Hibbard's gap sequence runs in ''O''(''N''5/4) average time, and that Gonnet and Baeza-Yates's sequence requires on average 0.41''N'' ln ''N'' (ln ln ''N'' + 1/6) element moves. Approximations of the average number of operations formerly put forward for other sequences fail when sorted arrays contain millions of elements. The graph below shows the average number of element comparisons use by various gap sequences, divided by the theoretical lower bound, i.e. log2''N''!. Ciuria's sequence 1, 4, 10, 23, 57, 132, 301, 701 (labelled Ci01) has been extended according to the formula . Applying the theory of Kolmogorov complexity, Jiang, Li, and Vitányi proved the following lower bound for the order of the average number of operations/running time in a ''p''-pass Shellsort: Ω(''pN''1+1/''p'') when ''p'' ≤ log2''N'' and Ω(''pN'') when ''p'' > log2''N''. Therefore, Shellsort has prospects of running in an average time that asymptotically grows like ''N'' log''N'' only when using gap sequences whose number of gaps grows in proportion to the logarithm of the array size. It is, however, unknown whether Shellsort can reach this asymptotic order of average-case complexity, which is optimal for comparison sorts. The lower bound was improved by Vitányi for every number of passes to where . This result implies for example the Jiang-Li-Vitányi lower bound for all -pass increment sequences and improves that lower bound for particular increment sequences. In fact all bounds (lower and upper) currently known for the average case are precisely matched by this lower bound. For example, this gives the new result that the Janson-Knuth upper bound is matched by the resulting lower bound for the used increment sequence, showing that three pass Shellsort for this increment sequence uses comparisons/inversions/running time. The formula allows us to search for increment sequences that yield lower bounds which are unknown; for example an increment sequence for four passes which has a lower bound greater than for the increment sequence . The lower bound becomes The worst-case complexity of any version of Shellsort is of higher order: Plaxton, Poonen, and Suel showed that it grows at least as rapidly as . Robert Cypher proved a stronger lower bound: when for all .Applications
Shellsort performs more operations and has higher cache miss ratio thanSee also
* Comb sortReferences
Bibliography
*External links
* – graphical demonstration