{"id":254,"date":"2026-11-13T12:36:01","date_gmt":"2026-11-13T04:36:01","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-shellsort-chooses-a-gap-sequence\/"},"modified":"2026-11-13T21:53:47","modified_gmt":"2026-11-13T13:53:47","slug":"how-shellsort-chooses-a-gap-sequence","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-shellsort-chooses-a-gap-sequence\/","title":{"rendered":"How Shellsort chooses a gap sequence"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Shellsort<\/strong> insertion-sorts the array at a descending sequence of gaps and finishes with gap 1. An h-sorted array stays h-sorted when a later pass uses a smaller gap. The worst-case bound is a property of the gap sequence: Shell&#8217;s original halving sequence is <strong>\u0398(n\u00b2)<\/strong>, Hibbard&#8217;s 2^k\u22121 sequence is <strong>O(n^{3\/2})<\/strong>, and Pratt&#8217;s products of powers of 2 and 3 are <strong>O(n log\u00b2 n)<\/strong>. The sort is in place.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Insertion sort moves an item one slot at a time. A first pass that sorts every h-th item moves distant inversions in long strides. Later passes repair finer gaps. The last gap has to be 1, or items in different residue classes never meet.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The design choice is the sequence h_t &gt; \u2026 &gt; h_1 = 1. A sequence that looks smooth can still force a quadratic number of moves. The bound has to be proved for that sequence, not assumed from the picture of diminishing strides.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Correctness does not depend on which descending sequence ends in 1. Insertion-sorting with gap h leaves each of the h subarrays sorted, and a later insertion sort with a smaller gap preserves that h-order. After gap 1 the whole array is sorted. Extra memory is O(1).<\/p>\n\n\n<p class=\"wp-block-paragraph\">The cost does depend on the sequence. Repeatedly taking floor(n\/2) yields a \u0398(n\u00b2) family of inputs. Hibbard&#8217;s gaps 2^k\u22121 bring the worst case down to O(n^{3\/2}). Pratt&#8217;s set of all 2^a 3^b below n brings it to O(n (log n)\u00b2), with more passes. Some of Sedgewick&#8217;s sequences are proved O(n^{4\/3}). Ciura&#8217;s short empirical sequence, starting 1, 4, 10, 23, 57, 132, 301, 701, is used because it performs well at practical sizes. The standard form is unstable, because a gap-h swap can leap over an equal key.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Choose a gap sequence that ends in 1. Store it once, or compute it from n.<\/li>\n<li>For each gap h from largest to smallest, insertion-sort every subarray a[i], a[i+h], a[i+2h], \u2026 .<\/li>\n<li>Inside a subarray, slide an item by strides of h until the item h steps left is smaller or equal, or the subarray begins.<\/li>\n<li>Leave equal keys unswapped inside the stride so that this pass at least keeps their order along the subarray.<\/li>\n<li>Finish with h = 1. That pass is an ordinary insertion sort on an array the earlier gaps have already cheapened.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The bounds above are comparison bounds for an in-place sort. They are not a license to pick gaps by eye. A sequence with a tidy formula can be the quadratic one. Pratt&#8217;s set is the classical sequence with an n log\u00b2 n proof, and it performs more passes than the sequences libraries actually tune.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the gap sequence is unproved<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Halve the length until the gap is 1<\/td>\n<td>This is Shell&#8217;s original sequence. There are families of inputs that still take \u0398(n\u00b2) comparisons.<\/td>\n<\/tr>\n<tr>\n<td>Any decreasing sequence of odd numbers<\/td>\n<td>Ending in 1 gives a correct sort. Oddness alone does not give a subquadratic proof.<\/td>\n<\/tr>\n<tr>\n<td>Cite Ciura&#8217;s sequence as O(n log n)<\/td>\n<td>That sequence is an experimental table for practical n. The n log n envelope is a different claim and needs a proof for the sequence in use.<\/td>\n<\/tr>\n<tr>\n<td>Expect stability<\/td>\n<td>A stride of h swaps nonadjacent items. An equal key between them stays put while the pair changes order.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">A proved O(n log\u00b2 n) in-place comparison sort of this family uses Pratt&#8217;s gaps. A proved O(n log n) in-place sort is heapsort. A stable library sort is timsort or mergesort.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">The Bash script and the Python script Shellsort the same integers with the original gap sequence n\/2, n\/4, \u2026. That sequence is the one whose worst case is quadratic. Both print <code>1 2 2 3 4<\/code>.<\/p>\n\n<pre><code class=\"language-bash\">a=(4 2 2 3 1)\nn=${#a[@]}\ngap=$((n\/2))\nwhile (( gap &gt; 0 )); do\n  for ((i=gap; i&lt;n; i++)); do\n    key=${a[i]}\n    j=$i\n    while (( j &gt;= gap &amp;&amp; a[j-gap] &gt; key )); do\n      a[j]=${a[j-gap]}\n      j=$((j-gap))\n    done\n    a[j]=$key\n  done\n  gap=$((gap\/2))\ndone\necho &quot;${a[*]}&quot;\n<\/code><\/pre>\n<pre><code class=\"language-python\">a = [4, 2, 2, 3, 1]\ngap = len(a) \/\/ 2\nwhile gap:\n    for i in range(gap, len(a)):\n        key = a[i]\n        j = i\n        while j &gt;= gap and a[j - gap] &gt; key:\n            a[j] = a[j - gap]\n            j -= gap\n        a[j] = key\n    gap \/\/= 2\nassert a == sorted(a)\nprint(*a)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Shellsort is an in-place gap sort still found in older and embedded libraries. Current standard libraries use introsort or timsort. The gap sequence is the whole design; a bad sequence loses the n log n behavior the caller expects.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop Shellsort when a caller needs a worst-case proof and the gap sequence does not have one. Use heapsort or mergesort. Drop it when equal keys must keep their order. Keep it when an in-place sort with short inner loops is wanted and the gap sequence is one whose bound, or whose measured range, matches the input sizes in front of you.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for Shellsort returns the gap sequence as the object that carries the complexity bound. <strong>Shellsort is correct for every gap sequence that ends at 1, and its worst-case cost is whatever that sequence has actually been proved to cost.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Shellsort insertion-sorts the array at a descending sequence of gaps and finishes with gap 1. An h-sorted array stays h-sorted when a later pass uses a smaller gap. The worst-case bound is a property of the gap sequence: Shell&#8217;s original halving sequence is \u0398(n\u00b2), Hibbard&#8217;s 2^k\u22121 sequence&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,31],"tags":[],"class_list":["post-254","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-sorting"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=254"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/254\/revisions"}],"predecessor-version":[{"id":653,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/254\/revisions\/653"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}