Shell Sort
- Ω( n log n )
- Θ( n4/3 )
- O( n2 )
- space O( 1 )
Shell sort is insertion sort with a head start. Before the final pass over neighbours, it insertion-sorts elements that sit a fixed gap apart, shrinking the gap each round, so small values cross long distances in one move instead of one slot at a time. This page runs it with Ciura's gaps 701, 301, 132, 57, 23, 10, 4 and 1, skipping every gap that is not smaller than the array length.
- Comparisons count only element-against-element checks; loop and index checks are never counted.
- Reads and writes include reads and writes to helper buffers (merge, counting, radix, bucket and tim sort).
- A swap counts once, as a swap. It is never also counted as a read or a write.
- Real time is the average time of the counted sort on this device over repeated runs, measured once before playback and independent of animation speed. Visual time is the time spent animating, excluding pauses.
How shell sort works
With six elements, every gap from 701 down to 10 is skipped, because the loop ignores any gap that is not smaller than n. That leaves 4 and 1. The gap-4 pass only has positions 4 and 5 to examine. The 4 at index 4 is smaller than the 5 four places to its left, so the 5 is copied right and the 4 is written into index 0, which gives 4, 2, 6, 1, 5, 3 after two writes. The 3 at index 5 is not smaller than the 2 at index 1, so it stays where it is.
The gap-1 pass is ordinary insertion sort on that array. The 2 pushes the 4 right and lands in front: 2, 4, 6, 1, 5, 3. The 6 stays. The 1 then travels all the way left, shifting 6, 4 and 2 one slot each, and the array reads 1, 2, 4, 6, 5, 3 once the 1 is written at index 0. The 5 steps in front of the 6, and finally the 3 moves 6, 5 and 4 one slot right and drops into index 2. The run costs 13 comparisons and 14 writes, 2 of them in the gap-4 pass and 12 in the gap-1 pass.
Shifting is done with writes, not swaps, so the trace table passes through states with a duplicated value, such as 2, 4, 6, 6, 5, 3, where the 6 has already been copied one slot right but the key waiting in a variable has not been placed yet. On this tiny input the gap-4 pass barely helped: it took the array from 9 inversions to 8. On larger arrays the early, wide passes do far more of the work and leave the last pass little to repair.
| Step | Change | Array after |
|---|---|---|
| 1 | Write 5 at index 4 | 5 2 6 1 5 3 |
| 2 | Write 4 at index 0 | 4 2 6 1 5 3 |
| 3 | Write 4 at index 1 | 4 4 6 1 5 3 |
| 4 | Write 2 at index 0 | 2 4 6 1 5 3 |
| 5 | Write 6 at index 3 | 2 4 6 6 5 3 |
| 6 | Write 4 at index 2 | 2 4 4 6 5 3 |
| 7 | Write 2 at index 1 | 2 2 4 6 5 3 |
| 8 | Write 1 at index 0 | 1 2 4 6 5 3 |
| 9 | Write 6 at index 4 | 1 2 4 6 6 3 |
| 10 | Write 5 at index 3 | 1 2 4 5 6 3 |
| 11 | Write 6 at index 5 | 1 2 4 5 6 6 |
| 12 | Write 5 at index 4 | 1 2 4 5 5 6 |
12 of 14 changes shown; the rest follow the same pattern until the array is sorted.
Pseudocode
for gap in [701, 301, 132, 57, 23, 10, 4, 1]
for i = gap to n - 1
key = a[i]
j = i
while j >= gap and key < a[j - gap]
a[j] = a[j - gap]
j = j - gap
a[j] = key
Why these complexities
On a sorted array no element ever moves. Each pass makes one comparison per element and writes nothing, so the cost is the number of active passes times n. With a gap sequence that shrinks geometrically that is n log n, the bound listed here. This implementation has a fixed table of at most eight gaps, so in practice a sorted input costs about 8n comparisons.
The listed average of n^(4/3) is an estimate, not a theorem. Nobody has proven the running time for Ciura's gaps, which were found by measuring rather than by derivation. The intuition behind the speed-up is that once a pass with gap g has finished, the array is g-sorted, and later passes with smaller gaps keep that property. Every later pass therefore repairs only a short stretch of disorder, and the final gap-1 pass meets few inversions.
The worst case is quadratic. The table stops at 701, so on a large array each chain for gap 701 holds about n/701 elements and is insertion-sorted, which costs on the order of n²/701 per pass. The constant is small but the shape is n². Space is constant: the loop keeps only i, j, the gap and the key, with no extra array. The sort is not stable, because a write across a gap can carry an element over an equal one.
Measured on this site
Exact counts from this site's own engine for 32 elements, using the same rules as the visualiser.
| Input | Comparisons | Writes | Swaps |
|---|---|---|---|
| Random | 154 | 129 | 0 |
| Already sorted | 90 | 0 | 0 |
| Reversed | 129 | 96 | 0 |
| Few distinct values | 112 | 54 | 0 |
When to use it
Use it for medium-sized arrays when you want a short, in-place sort without recursion that is much faster than insertion sort. It is common in small libraries and embedded code.
Frequently asked questions
What is the gap sequence?
The list of distances between compared elements. This version uses Ciura's sequence, found by experiment, and always ends with 1.
Why is the average case only an estimate?
The exact running time for good gap sequences is an open problem, so the listed average is an approximation.
Is Shell sort stable?
No. Moving elements across a gap can reorder equal elements.
Why does this version skip gaps instead of starting at n/2?
The gaps come from a fixed table, and the loop skips any gap that is not smaller than the array length. Six elements use only 4 and 1. A hundred elements use 57, 23, 10, 4 and 1. All eight gaps are smaller than 1000, the largest array this site sorts, so a full-size run uses every one of them.
Can Shell sort do more work than plain insertion sort?
Yes, on very small or nearly sorted arrays. On the six-value trace, plain insertion sort needs 13 writes (9 shifts and 4 key placements), while Shell sort needs 14. The extra passes only pay off once the array is big enough for long jumps to save many single-slot moves.
Background
Donald Shell published the method in 1959 in Communications of the ACM, under the title A High-Speed Sorting Procedure. His own sequence halved the gap each round, n/2, n/4 and so on down to 1. That is easy to code but still quadratic in the worst case, because with power-of-two gaps the elements at even and odd positions never meet until the last pass. Research then moved to the gaps: sequences of the form 2ᵏ − 1 were shown in the 1960s to give n^(3/2), and Pratt's 1971 sequence of products of powers of 2 and 3 gives O(n log² n), though with so many passes that it is slow in practice.
Marcin Ciura's 2001 paper searched for the best gaps for the average case and arrived at 1, 4, 10, 23, 57, 132, 301, 701. Programs that sort larger arrays usually grow the sequence beyond 701 by multiplying the last gap by about 2.25. Shell sort needs no recursion and no allocation, which is why it still shows up in small C libraries and firmware. Knuth analyses it in section 5.2.1.
References
- Knuth, The Art of Computer Programming, vol. 3, section 5.2.1 (Sorting by Insertion, Shellsort)
- Shell, A High-Speed Sorting Procedure, Communications of the ACM 2(7), 1959
- Ciura, Best Increments for the Average Case of Shellsort, Fundamentals of Computation Theory, 2001
Compare with
| Algorithm | Average time | Worst time |
|---|---|---|
| Insertion Sort | Θ( n2 ) | O( n2 ) |
| Best time | Ω( n log n ) | good |
|---|---|---|
| Average time | Θ( n4/3 ) | fair |
| Worst time | O( n2 ) | poor |
| Worst space | O( 1 ) | good |
Properties
- Stable
- No
- In place
- Yes
- Adaptive
- Yes
- Comparison-based
- Yes