|
|
Line 1: |
Line 1: |
| {{Infobox Algorithm
| | Nothing to say about myself really.<br>Enjoying to be a part of wmflabs.org.<br>I really wish I am useful at all<br><br>Feel free to surf to my homepage; [http://safedietsthatwork.webs.com diet plans] |
| |class=[[Sorting algorithm]]
| |
| |image=[[File:Sorting shellsort anim.gif|Step-by-step visualisation of Shellsort]]<br/><small>Shellsort with gaps 23, 10, 4, 1 in action.</small>
| |
| |data=[[Array data structure|Array]]
| |
| |time=depends on gap sequence
| |
| |best-time=depends on gap sequence
| |
| |average-time=depends on gap sequence
| |
| |space=О(n) total, O(1) auxiliary
| |
| |optimal=No
| |
| }}
| |
| '''Shellsort''', also known as '''Shell sort''' or '''Shell's method''', is an in-place [[comparison sort]]. It generalizes an exchanging sort, such as [[insertion sort|insertion]] or [[bubble sort|bubble]] sort, by starting the comparison and exchange of elements with elements that are far apart before finishing with neighboring elements. Starting with far apart elements can move some out-of-place elements into position faster than a simple nearest neighbor exchange. [[Donald Shell]] published the first version of this sort in 1959.<ref name="Shell">{{Cite journal
| |
| |url=http://penguin.ewu.edu/cscd300/Topic/AdvSorting/p30-shell.pdf
| |
| |last=Shell
| |
| |first=D.L.
| |
| |title=A High-Speed Sorting Procedure
| |
| |journal=Communications of the ACM
| |
| |volume=2
| |
| |issue=7
| |
| |year=1959
| |
| |pages=30–32
| |
| |doi=10.1145/368370.368387}}</ref><ref>Some older textbooks and references call this the "Shell-Metzner" sort after [[Marlene Metzner Norton]], but according to Metzner, "I had nothing to do with the sort, and my name should never have been attached to it." See {{Cite web
| |
| |title=Shell sort
| |
| |publisher=National Institute of Standards and Technology
| |
| |url=http://www.nist.gov/dads/HTML/shellsort.html
| |
| |accessdate=2007-07-17 }}</ref> The running time of Shellsort is heavily dependent on the gap sequence it uses. For many practical variants, determining their [[time complexity]] remains an [[open problem]].
| |
| | |
| == Description ==
| |
| Shellsort is a generalization 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, considering every ''h''th element gives a sorted list. Such a list is said to be ''h''-sorted. Equivalently, it can be thought of as ''h'' interleaved lists, each individually sorted.<ref name="Sedgewick">{{Cite book
| |
| |last=Sedgewick
| |
| |first=Robert
| |
| |authorlink=Robert Sedgewick (computer scientist)
| |
| |title=Algorithms in C
| |
| |edition=3rd
| |
| |volume=1
| |
| |publisher=Addison-Wesley
| |
| |location=
| |
| |year=1998
| |
| |pages=273-281
| |
| |isbn=0-201-31452-5}}</ref> Beginning with large values of ''h'', this rearrangement 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.<ref name="KR">{{Cite book
| |
| |last1=Kernighan
| |
| |first1=Brian W.
| |
| |authorlink1=Brian Kernighan
| |
| |last2=Ritchie
| |
| |first2=Dennis M.
| |
| |authorlink2=Dennis Ritchie
| |
| |title=The C Programming Language
| |
| |edition=2nd
| |
| |publisher=Prentice Hall
| |
| |year=1996
| |
| |pages=62
| |
| |isbn=7-302-02412-X}}</ref> If the file is then ''k-sorted'' for some smaller integer ''k'', then the file remains ''h''-sorted. Following this idea for a decreasing sequence of ''h'' values ending in 1 is guaranteed to leave a sorted list in the end.<ref name="Sedgewick"/>
| |
| | |
| An example run of Shellsort with gaps 5, 3 and 1 is shown below.
| |
| | |
| <math> | |
| \begin{array}{rcccccccccccc}
| |
| &a_1&a_2&a_3&a_4&a_5&a_6&a_7&a_8&a_9&a_{10}&a_{11}&a_{12}\\
| |
| \hbox{input data:}
| |
| & 62& 83& 18& 53& 07& 17& 95& 86& 47& 69& 25& 28\\
| |
| \hbox{after 5-sorting:}
| |
| & 17& 28& 18& 47& 07& 25& 83& 86& 53& 69& 62& 95\\
| |
| \hbox{after 3-sorting:}
| |
| & 17& 07& 18& 47& 28& 25& 69& 62& 53& 83& 86& 95\\
| |
| \hbox{after 1-sorting:}
| |
| & 07& 17& 18& 25& 28& 47& 53& 62& 69& 83& 86& 95\\
| |
| \end{array}
| |
| </math> | |
| | |
| The first pass, 5-sorting, performs insertion sort on separate subarrays (''a''<sub>1</sub>, ''a''<sub>6</sub>, ''a''<sub>11</sub>), (''a''<sub>2</sub>, ''a''<sub>7</sub>, ''a''<sub>12</sub>), (''a''<sub>3</sub>, ''a''<sub>8</sub>), (''a''<sub>4</sub>, ''a''<sub>9</sub>), (''a''<sub>5</sub>, ''a''<sub>10</sub>). For instance, it changes the subarray (''a''<sub>1</sub>, ''a''<sub>6</sub>, ''a''<sub>11</sub>) from (62, 17, 25) to (17, 25, 62). The next pass, 3-sorting, performs insertion sort on the subarrays (''a''<sub>1</sub>, ''a''<sub>4</sub>, ''a''<sub>7</sub>, ''a''<sub>10</sub>), (''a''<sub>2</sub>, ''a''<sub>5</sub>, ''a''<sub>8</sub>, ''a''<sub>11</sub>), (''a''<sub>3</sub>, ''a''<sub>6</sub>, ''a''<sub>9</sub>, ''a''<sub>12</sub>). The last pass, 1-sorting, is an ordinary insertion sort of the entire array (''a''<sub>1</sub>,..., ''a''<sub>12</sub>).
| |
| | |
| 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.
| |
| | |
| Shellsort is [[sorting algorithm#Stability|unstable]]: it may change the relative order of elements with equal values. It has "natural" behavior, in that it executes faster when the input is partially sorted.
| |
| | |
| == Pseudocode ==
| |
| Using Marcin Ciura's gap sequence, with an inner insertion sort.
| |
| | |
| ''# Sort an array a[0...n-1].''
| |
| gaps = [701, 301, 132, 57, 23, 10, 4, 1]
| |
|
| |
| '''[[foreach]]''' (gap in gaps)
| |
| {
| |
| ''# Do an insertion sort for each gap size.''
| |
| '''for''' (i = gap; i < n; i += 1)
| |
| {
| |
| temp = a[i]
| |
| '''for''' (j = i; j >= gap and a[j - gap] > temp; j -= gap)
| |
| {
| |
| a[j] = a[j - gap]
| |
| }
| |
| a[j] = temp
| |
| }
| |
|
| |
| }
| |
| | |
| == Gap sequences ==
| |
| The question of deciding which gap sequence to use is difficult. Every gap sequence that contains 1 yields a correct sort; however, the properties of thus obtained versions of Shellsort may be very different.
| |
| | |
| 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.
| |
| | |
| :{| class="wikitable"
| |
| |- style="background-color: #efefef;"
| |
| ! General term (''k'' ≥ 1)
| |
| ! Concrete gaps
| |
| ! Worst-case<br>time complexity
| |
| ! Author and year of publication
| |
| |----
| |
| | <math>\lfloor N / 2^k \rfloor</math>
| |
| | <math>\left\lfloor\frac{N}{2}\right\rfloor,
| |
| \left\lfloor\frac{N}{4}\right\rfloor, \ldots, 1</math>
| |
| | <math>\Theta(N^2)</math> [when ''N''=2<sup>''p''</sup>]
| |
| | [[Donald Shell|Shell]], 1959<ref name="Shell"/>
| |
| |----
| |
| | <math>2 \lfloor N / 2^{k+1} \rfloor + 1</math>
| |
| | <math>2 \left\lfloor\frac{N}{4}\right\rfloor + 1, \ldots, 3, 1</math>
| |
| | <math>\Theta(N^{3/2})</math>
| |
| | Frank & Lazarus, 1960<ref>{{Cite journal
| |
| |last=Frank
| |
| |first=R.M.
| |
| |last2=Lazarus
| |
| |first2=R.B.
| |
| |title=A High-Speed Sorting Procedure
| |
| |journal=Communications of the ACM
| |
| |volume=3
| |
| |issue=1
| |
| |year=1960
| |
| |pages=20–22
| |
| |doi=10.1145/366947.366957}}</ref>
| |
| |----
| |
| | <math>2^k - 1</math>
| |
| | <math>1, 3, 7, 15, 31, 63, \ldots</math>
| |
| | <math>\Theta(N^{3/2})</math>
| |
| | Hibbard, 1963<ref>{{Cite journal
| |
| |last=Hibbard
| |
| |first=Thomas N.
| |
| |title=An Empirical Study of Minimal Storage Sorting
| |
| |journal=Communications of the ACM
| |
| |volume=6
| |
| |issue=5
| |
| |year=1963
| |
| |pages=206–213
| |
| |doi=10.1145/366552.366557}}</ref>
| |
| |----
| |
| | <math>2^k + 1</math>, prefixed with 1
| |
| | <math>1, 3, 5, 9, 17, 33, 65, \ldots</math>
| |
| | <math>\Theta(N^{3/2})</math>
| |
| | Papernov & Stasevich, 1965<ref>{{Cite journal
| |
| |url=http://www.mathnet.ru/links/83f0a81df1ec06f76d3683c6cab7d143/ppi751.pdf
| |
| |last=Papernov
| |
| |first=A.A.
| |
| |last2=Stasevich
| |
| |first2=G.V.
| |
| |title=A Method of Information Sorting in Computer Memories
| |
| |journal=Problems of Information Transmission
| |
| |volume=1
| |
| |issue=3
| |
| |year=1965
| |
| |pages=63–75}}</ref>
| |
| |----
| |
| | successive numbers of the form <math>2^p 3^q</math>
| |
| | <math>1, 2, 3, 4, 6, 8, 9, 12, \ldots</math>
| |
| | <math>\Theta(N \log^2 N)</math>
| |
| | [[Vaughan Ronald Pratt|Pratt]], 1971<ref>{{Cite book
| |
| |last=Pratt
| |
| |first=Vaughan Ronald
| |
| |year=1979
| |
| |publisher=Garland
| |
| |title=Shellsort and Sorting Networks (Outstanding Dissertations in the Computer Sciences)
| |
| |isbn=0-8240-4406-1}}</ref>
| |
| |----
| |
| | <math>(3^k - 1) / 2</math>, not greater than <math>\lceil N / 3 \rceil</math>
| |
| | <math>1, 4, 13, 40, 121, \ldots </math>
| |
| | <math>\Theta(N^{3/2})</math>
| |
| | [[Donald Knuth|Knuth]], 1973<ref name="Knuth">{{Cite book
| |
| |last=Knuth
| |
| |first=Donald E.
| |
| |title=The Art of Computer Programming. Volume 3: Sorting and Searching
| |
| |edition=2nd
| |
| |publisher=Addison-Wesley
| |
| |location=Reading, Massachusetts
| |
| |year=1997
| |
| |pages=83–95
| |
| |chapter=Shell's method
| |
| |isbn=0-201-89685-0}}</ref>
| |
| |----
| |
| | <math>\prod
| |
| \limits_{\scriptscriptstyle 0\le q<r\atop
| |
| \scriptscriptstyle q\neq(r^2+r)/2-k}a_q, \hbox{where}</math>
| |
| :<math>r = \left\lfloor \sqrt{2k+\sqrt{2k}} \right\rfloor,</math>
| |
| :<math>a_q=\min\{n\in\mathbb{N}\colon n\ge(5/2)^{q+1},</math>
| |
| :<math>\forall p\colon0\le p<q\Rightarrow\gcd(a_p,n)=1\}</math>
| |
| | <math>1, 3, 7, 21, 48, 112, \ldots</math>
| |
| | <math>O(N e^\sqrt{8\ln(5/2)\ln N})</math>
| |
| | Incerpi & [[Robert Sedgewick (computer scientist)|Sedgewick]], 1985<ref>{{Cite journal
| |
| |last=Incerpi
| |
| |first=Janet
| |
| |last2=Sedgewick
| |
| |first2=Robert
| |
| |title=Improved Upper Bounds on Shellsort
| |
| |journal=Journal of Computer and System Sciences
| |
| |volume=31
| |
| |issue=2
| |
| |year=1985
| |
| |pages=210–224
| |
| |doi=}}</ref>
| |
| |----
| |
| | <math>4^k + 3\cdot2^{k-1} + 1</math>, prefixed with 1
| |
| | <math>1, 8, 23, 77, 281, \ldots</math>
| |
| | <math>O(N^{4/3})</math>
| |
| | Sedgewick, 1986<ref name="Sedgewick"/>
| |
| |----
| |
| | <math>9(4^{k-1}-2^{k-1})+1, 4^{k+1}-6\cdot2^k+1</math>
| |
| | <math>1, 5, 19, 41, 109, \ldots</math>
| |
| | <math>O(N^{4/3})</math>
| |
| | Sedgewick, 1986<ref name="Sedgewick">{{Cite journal
| |
| |last=Sedgewick
| |
| |first=Robert
| |
| |title=A New Upper Bound for Shellsort
| |
| |journal=Journal of Algorithms
| |
| |volume=7
| |
| |issue=2
| |
| |year=1986
| |
| |pages=159–173
| |
| |doi=10.1016/0196-6774(86)90001-5}}</ref>
| |
| |----
| |
| | <math>h_k = \max\left\{\left\lfloor 5h_{k-1}/11 \right\rfloor, 1\right\}, h_0 = N</math>
| |
| | <math>\left\lfloor \frac{5N}{11} \right\rfloor, \left\lfloor \frac{5}{11}\left\lfloor \frac{5N}{11} \right\rfloor\right\rfloor, \ldots, 1</math>
| |
| | ?
| |
| | [[Gaston Gonnet|Gonnet]] & [[Ricardo Baeza-Yates|{{j|Baeza-Yates}}]], 1991<ref name="Gonnet">{{Cite book
| |
| |last=Gonnet
| |
| |first=Gaston H.
| |
| |last2=Baeza-Yates
| |
| |first2=Ricardo
| |
| |title=Handbook of Algorithms and Data Structures: In Pascal and C
| |
| |publisher=Addison-Wesley
| |
| |location=Reading, Massachusetts
| |
| |edition=2nd
| |
| |year=1991
| |
| |pages=161–163
| |
| |chapter=Shellsort
| |
| |isbn=0-201-41607-7}}</ref>
| |
| |----
| |
| | <math>\left\lceil \frac{9^k-4^k}{5\cdot4^{k-1}} \right\rceil</math>
| |
| | <math>1, 4, 9, 20, 46, 103, \ldots</math>
| |
| | ?
| |
| | Tokuda, 1992<ref>{{Cite book
| |
| |editor-last=van Leeuven
| |
| |editor-first=Jan
| |
| |chapter=An Improved Shellsort
| |
| |last=Tokuda
| |
| |first=Naoyuki
| |
| |title=Proceedings of the IFIP 12th World Computer Congress on Algorithms, Software, Architecture
| |
| |publisher=North-Holland Publishing Co.
| |
| |location=Amsterdam
| |
| |year=1992
| |
| |pages=449–457
| |
| |isbn=0-444-89747-X}}</ref>
| |
| |----
| |
| | unknown
| |
| | <math>1, 4, 10, 23, 57, 132, 301, 701</math>
| |
| | ?
| |
| | Ciura, 2001<ref>{{Cite book
| |
| |url=http://sun.aei.polsl.pl/~mciura/publikacje/shellsort.pdf
| |
| |title=Proceedings of the 13th International Symposium on Fundamentals of Computation Theory
| |
| |editor-last=Freiwalds
| |
| |editor-first=Rusins
| |
| |last=Ciura
| |
| |first=Marcin
| |
| |chapter=Best Increments for the Average Case of Shellsort
| |
| |publisher=Springer-Verlag
| |
| |location=London
| |
| |year=2001
| |
| |pages=106–117
| |
| |isbn=3-540-42487-3}}</ref>
| |
| |}
| |
| When the binary representation of ''N'' contains many consecutive zeroes, Shellsort using Shell's original gap sequence makes Θ(''N''<sup>2</sup>) 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 network]]s 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.<ref name="Gonnet"/> 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 to use gaps that have low [[greatest common divisor]]s or are pairwise [[coprime]].<ref>{{Cite book
| |
| |title=Algorithms in C++, Parts 1-4: Fundamentals, Data Structure, Sorting, Searching
| |
| |last=Sedgewick
| |
| |first=Robert
| |
| |chapter=Shellsort
| |
| |publisher=Addison-Wesley
| |
| |location=Reading, Massachusetts
| |
| |year=1998
| |
| |pages=285–292
| |
| |isbn=0-201-35088-2}}</ref>
| |
| | |
| With respect to the average number of comparisons, the best known gap sequences are 1, 4, 10, 23, 57, 132, 301, 701 and similar, with gaps found experimentally. Optimal gaps beyond 701 remain unknown, but good results can be obtained by extending the above sequence according to the recursive formula <math>h_k = \lfloor 2.25 h_{k-1} \rfloor</math>.
| |
| | |
| Tokuda's sequence, defined by the simple formula <math>h_k = \lceil h'_k \rceil</math>, where <math>h'_k = 2.25 h'_{k-1} + 1</math>, <math>h'_1 = 1</math>, can be recommended for practical applications.
| |
| | |
| == Computational complexity ==
| |
| The following property holds: after ''h''<sub>2</sub>-sorting of any ''h''<sub>1</sub>-sorted array, the array remains ''h''<sub>1</sub>-sorted.<ref>{{Cite journal
| |
| |last=Gale
| |
| |first=David
| |
| |last2=Karp
| |
| |first2=Richard M.
| |
| |title=A Phenomenon in the Theory of Sorting
| |
| |journal=Journal of Computer and System Sciences
| |
| |volume=6
| |
| |issue=2
| |
| |year=1972
| |
| |pages=103–115
| |
| |doi=10.1016/S0022-0000(72)80016-3}}</ref> Every ''h''<sub>1</sub>-sorted and ''h''<sub>2</sub>-sorted array is also (''a''<sub>1</sub>''h''<sub>1</sub>+''a''<sub>2</sub>''h''<sub>2</sub>)-sorted, for any nonnegative integers ''a''<sub>1</sub> and ''a''<sub>2</sub>. The worst-case complexity of Shellsort is therefore connected with the [[coin problem|Frobenius problem]]: for given integers ''h''<sub>1</sub>,..., ''h''<sub>''n''</sub> with gcd = 1, the Frobenius number ''g''(''h''<sub>1</sub>,..., ''h''<sub>''n''</sub>) is the greatest integer that cannot be represented as ''a''<sub>1</sub>''h''<sub>1</sub>+ ... +''a''<sub>''n''</sub>''h''<sub>''n''</sub> with nonnegative integer ''a''<sub>1</sub>,..., ''a''<sub>''n''</sub>. Using known formulae for Frobenius numbers, we can determine the worst-case complexity of Shellsort for several classes of gap sequences.<ref>{{Cite journal
| |
| |last=Selmer
| |
| |first=Ernst S.
| |
| |title=On Shellsort and the Frobenius Problem
| |
| |journal=BIT Numerical Mathematics
| |
| |volume=29
| |
| |issue=1
| |
| |year=1989
| |
| |pages=37–40
| |
| |doi=10.1007/BF01932703}}</ref> Proven results are shown in the above table.
| |
| | |
| With respect to the average number of operations, none of proven results concerns a practical gap sequence. For gaps that are powers of two, Espelid computed this average as <math>0.5349N\sqrt{N}-0.4387N-0.097\sqrt{N}+O(1)</math>.<ref>{{Cite journal
| |
| |last=Espelid
| |
| |first=Terje O.
| |
| |title=Analysis of a Shellsort Algorithm
| |
| |journal=BIT Numerical Mathematics
| |
| |volume=13
| |
| |issue=4
| |
| |year=1973
| |
| |pages=394–400
| |
| |doi=10.1007/BF01933401}}</ref> [[Donald Knuth|Knuth]] determined the average complexity of sorting an ''N''-element array with two gaps (''h'', 1) to be <math>2N^2/h + \sqrt{\pi N^3 h}</math>.<ref name="Knuth" /> It follows that a two-pass Shellsort with ''h'' = Θ(''N''<sup>1/3</sup>) makes on average ''O''(''N''<sup>5/3</sup>) comparisons. [[Andrew Yao|Yao]] found the average complexity of a three-pass Shellsort.<ref>{{Cite journal
| |
| |last=Yao
| |
| |first=Andrew Chi-Chih
| |
| |title=An Analysis of (''h'', ''k'', 1)-Shellsort
| |
| |journal=Journal of Algorithms
| |
| |volume=1
| |
| |issue=1
| |
| |year=1980
| |
| |pages=14–50
| |
| |doi=10.1016/0196-6774(80)90003-6}}</ref> His result was refined by Janson and Knuth:<ref>{{Cite journal
| |
| |arxiv=cs/9608105
| |
| |id=[[CiteSeerX]]: {{url|1=citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.54.9911|2=10.1.1.54.9911}}
| |
| |last=Janson
| |
| |first=Svante
| |
| |last2=Knuth
| |
| |first2=Donald E.
| |
| |title=Shellsort with Three Increments
| |
| |journal=Random Structures and Algorithms
| |
| |volume=10
| |
| |issue=1-2
| |
| |year=1997
| |
| |pages=125–142
| |
| |doi=10.1002/(SICI)1098-2418(199701/03)10:1/2<125::AID-RSA6>3.0.CO;2-X}}</ref> the average number of comparisons made during a Shellsort with three gaps (''ch'', ''cg'', 1), where ''h'' and ''g'' are coprime, is <math>\frac{N^2}{4ch}+O(N)</math> in the first pass, <math>\frac{1}{8g}\sqrt{\frac{\pi}{ch}}(h-1)N^{3/2}+O(hN)</math> in the second pass and <math>\psi(h, g)N + \frac{1}{8}\sqrt{\frac{\pi}{c}}(c-1)N^{3/2}+O((c-1)gh^{1/2}N) + O(c^2g^3h^2)</math> in the third pass. ''ψ''(''h'', ''g'') in the last formula is a complicated function asymptotically equal to <math>\sqrt{\frac{\pi h}{128}}g + O(g^{-1/2}h^{1/2}) + O(gh^{-1/2})</math>. In particular, when ''h'' = Θ(''N''<sup>7/15</sup>) and ''g'' = Θ(''h''<sup>1/5</sup>), the average time of sorting is ''O''(''N''<sup>23/15</sup>).
| |
| | |
| Based on experiments, it is conjectured that Shellsort with Hibbard's and Knuth's gap sequences runs in ''O''(''N''<sup>5/4</sup>) average time,<ref name="Knuth" /> and that Gonnet and Baeza-Yates's sequence requires on average 0.41''N''ln''N''(ln ln''N''+1/6) element moves.<ref name="Gonnet" /> 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 in various variants of Shellsort, divided by the theoretical lower bound, i.e. log<sub>2</sub>''N''!, where the sequence 1, 4, 10, 23, 57, 132, 301, 701 has been extended according to the formula <math>h_k = \lfloor2.25 h_{k-1}\rfloor</math>.
| |
| | |
| [[File:Shell sort average number of comparisons (English).svg|center]]
| |
| | |
| Applying the theory of [[Kolmogorov complexity]], Jiang, Li, and [[Paul Vitanyi|Vitányi]] proved the following lower bounds for the order of the average number of operations in an ''m''-pass Shellsort: Ω(''mN''<sup>1+1/''m''</sup>) when ''m''≤log<sub>2</sub>''N'' and Ω(''mN'') when ''m''>log<sub>2</sub>''N''.<ref>{{Cite journal
| |
| |id = [[CiteSeerX]]: {{url|1=citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.6.6508|2=10.1.1.6.6508}}
| |
| |last=Jiang
| |
| |first=Tao
| |
| |last2=Li
| |
| |first2=Ming
| |
| |last3=Vitányi
| |
| |first3=Paul
| |
| |title=A Lower Bound on the Average-Case Complexity of Shellsort
| |
| |journal=[[Journal of the ACM]]
| |
| |volume=47
| |
| |issue=5
| |
| |year=2000
| |
| |pages=905–911
| |
| |doi=10.1145/355483.355488}}</ref> 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 worst-case complexity of any version of Shellsort is of higher order: Plaxton, Poonen, and [[Torsten Suel|Suel]] showed that it grows at least as rapidly as Ω(''N''(log''N''/log log''N'')<sup>2</sup>).<ref>{{Cite journal
| |
| |id=[[CiteSeerX]]: {{url|1=citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.43.1393|2=10.1.1.43.1393}}
| |
| |last=Plaxton
| |
| |first=C. Greg
| |
| |last2=Poonen
| |
| |first2=Bjarne
| |
| |last3=Suel
| |
| |first3=Torsten
| |
| |title=Improved Lower Bounds for Shellsort
| |
| |journal=Annual Symposium on Foundations of Computer Science
| |
| |volume=33
| |
| |year=1992
| |
| |pages=226–235
| |
| |doi=10.1109/SFCS.1992.267769
| |
| }}</ref>
| |
| | |
| == Applications ==
| |
| Shellsort is now rarely used in serious applications. It performs more operations and has higher [[CPU cache#Cache_miss|cache miss ratio]] than [[quicksort]]. However, since it can be implemented using little code and does not use the [[call stack]], some implementations of the [[qsort]] function in the [[C standard library]] targeted at [[embedded systems]] use it instead of quicksort. Shellsort is, for example, used in the [[uClibc]] library.<ref>{{Cite web
| |
| | url=http://git.uclibc.org/uClibc/tree/libc/stdlib/stdlib.c#n786
| |
| | title=libc/stdlib/stdlib.c
| |
| | author=Manuel Novoa III
| |
| | accessdate=2011-03-30}}</ref> For similar reasons, an implementation of Shellsort is present in the [[Linux kernel]].<ref>{{Cite web
| |
| | url=https://github.com/torvalds/linux/blob/72932611b4b05bbd89fafa369d564ac8e449809b/kernel/groups.c#L105
| |
| | title=kernel/groups.c
| |
| | accessdate=2012-05-05}}</ref>
| |
| | |
| Shellsort can also serve as a sub-algorithm of [[introsort|introspective sort]], to sort short subarrays and to prevent a pathological slowdown when the recursion depth exceeds a given limit. This principle is employed, for instance, in the [[bzip2]] compressor.<ref>{{Cite web
| |
| |url=http://www.ncbi.nlm.nih.gov/IEB/ToolBox/CPP_DOC/lxr/source/src/util/compress/bzip2/blocksort.c#L519
| |
| |title=bzip2/blocksort.c
| |
| |author=Julian Seward
| |
| |accessdate=2011-03-30}}</ref>
| |
| | |
| == See also ==
| |
| * [[Comb sort]]
| |
| | |
| == References ==
| |
| {{reflist|30em}}
| |
| | |
| == Bibliography ==
| |
| * {{Cite book
| |
| |last=Knuth
| |
| |first=Donald E.
| |
| |title=The Art of Computer Programming. Volume 3: Sorting and Searching
| |
| |edition=2nd
| |
| |publisher=Addison-Wesley
| |
| |location=Reading, Massachusetts
| |
| |year=1997
| |
| |pages=83–95
| |
| |chapter=Shell's method
| |
| |isbn=0-201-89685-0
| |
| }}
| |
| * [http://www.cs.princeton.edu/~rs/shell/ Analysis of Shellsort and Related Algorithms], Robert Sedgewick, Fourth European Symposium on Algorithms, Barcelona, September 1996.
| |
| | |
| == External links ==
| |
| {{wikibooks|Algorithm implementation|Sorting/Shell_sort|Shell sort}}
| |
| * [http://www.youtube.com/watch?v=CmPA7zE8mx0 Shellsort with gaps 5, 3, 1 as a Hungarian folk dance]
| |
| | |
| {{sorting}}
| |
| {{Use dmy dates|date=September 2010}}
| |
| | |
| {{DEFAULTSORT:Shellsort}}
| |
| [[Category:Sorting algorithms]]
| |
| [[Category:Comparison sorts]]
| |
| | |
| {{link GA|pl}}
| |