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’s original halving sequence is Θ(n²), Hibbard’s 2^k−1 sequence is O(n^{3/2}), and Pratt’s products of powers of 2 and 3 are O(n log² n). The sort is in place.
What the problem actually asks
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.
The design choice is the sequence h_t > … > 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.
What the algorithm guarantees
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).
The cost does depend on the sequence. Repeatedly taking floor(n/2) yields a Θ(n²) family of inputs. Hibbard’s gaps 2^k−1 bring the worst case down to O(n^{3/2}). Pratt’s set of all 2^a 3^b below n brings it to O(n (log n)²), with more passes. Some of Sedgewick’s sequences are proved O(n^{4/3}). Ciura’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.
The working steps
- Choose a gap sequence that ends in 1. Store it once, or compute it from n.
- For each gap h from largest to smallest, insertion-sort every subarray a[i], a[i+h], a[i+2h], … .
- Inside a subarray, slide an item by strides of h until the item h steps left is smaller or equal, or the subarray begins.
- Leave equal keys unswapped inside the stride so that this pass at least keeps their order along the subarray.
- Finish with h = 1. That pass is an ordinary insertion sort on an array the earlier gaps have already cheapened.
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’s set is the classical sequence with an n log² n proof, and it performs more passes than the sequences libraries actually tune.
Approaches that look simpler and fail
| Approach | Why it fails when the gap sequence is unproved |
|---|---|
| Halve the length until the gap is 1 | This is Shell’s original sequence. There are families of inputs that still take Θ(n²) comparisons. |
| Any decreasing sequence of odd numbers | Ending in 1 gives a correct sort. Oddness alone does not give a subquadratic proof. |
| Cite Ciura’s sequence as O(n log n) | 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. |
| Expect stability | A stride of h swaps nonadjacent items. An equal key between them stays put while the pair changes order. |
A proved O(n log² n) in-place comparison sort of this family uses Pratt’s gaps. A proved O(n log n) in-place sort is heapsort. A stable library sort is timsort or mergesort.
A working example
The Bash script and the Python script Shellsort the same integers with the original gap sequence n/2, n/4, …. That sequence is the one whose worst case is quadratic. Both print 1 2 2 3 4.
a=(4 2 2 3 1)
n=${#a[@]}
gap=$((n/2))
while (( gap > 0 )); do
for ((i=gap; i<n; i++)); do
key=${a[i]}
j=$i
while (( j >= gap && a[j-gap] > key )); do
a[j]=${a[j-gap]}
j=$((j-gap))
done
a[j]=$key
done
gap=$((gap/2))
done
echo "${a[*]}"
a = [4, 2, 2, 3, 1]
gap = len(a) // 2
while gap:
for i in range(gap, len(a)):
key = a[i]
j = i
while j >= gap and a[j - gap] > key:
a[j] = a[j - gap]
j -= gap
a[j] = key
gap //= 2
assert a == sorted(a)
print(*a)
Where it is used
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.
When you can drop this algorithm
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.
Why this stays published
We publish this so a search for Shellsort returns the gap sequence as the object that carries the complexity bound. 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.
本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。
