CZ
CalcyZone
Computer Science & Software CalcyZone Calculator

Shell Sort Visualizer (Step-by-Step)

Animate Shell sort with shrinking gaps and see each gapped insertion move.

Sorted
1, 2, 3, 8, 12, 23, 34, 54
Element moves
9
Steps
12
Step 1 / 12

Start with gap n/2 (Shell’s original sequence).

Step-by-step working

  1. Start with gap = ⌊n/2⌋ and gapped insertion sort.
  2. Halve the gap until it is 1 (final pass is plain insertion sort).
  3. Press Play or Step to watch every move.

Mathematical Formula

Gaps n/2, n/4, …, 1 (Shell, 1959). For each gap, perform insertion sort on elements gap apart. Worst case O(n²) for this sequence.

Overview & Explanation

Shell sort generalises insertion sort by first moving elements far apart, so the final pass has little work.

How It Works

  • Pick a gap.
  • Insertion-sort each gap-separated subsequence.
  • Halve the gap until 1.

Practical Applications

  • Understanding gap sequences.
  • Embedded systems with small arrays.

Example 1: 23, 12, 1, 8, 34, 54, 2, 3

Eight elements.

1

Inputs

Array to sort (comma separated, max 40): 23, 12, 1, 8, 34, 54, 2, 3

= entered
2

Step 1

Start with gap = ⌊n/2⌋ and gapped insertion sort.

3

Step 2

Halve the gap until it is 1 (final pass is plain insertion sort).

4

Step 3

Press Play or Step to watch every move.

5

Sorted

Result

= 1, 2, 3, 8, 12, 23, 34, 54

Example 2: reverse order

Worst-ish input.

1

Inputs

Array to sort (comma separated, max 40): 9, 8, 7, 6, 5, 4, 3, 2, 1

= entered
2

Step 1

Start with gap = ⌊n/2⌋ and gapped insertion sort.

3

Step 2

Halve the gap until it is 1 (final pass is plain insertion sort).

4

Step 3

Press Play or Step to watch every move.

5

Sorted

Result

= 1, 2, 3, 4, 5, 6, 7, 8, 9

How it's calculated & sources

Gaps n/2, n/4, …, 1 (Shell, 1959). For each gap, perform insertion sort on elements gap apart. Worst case O(n²) for this sequence.

Frequently Asked Questions

Which gap sequence is best?
Ciura’s (1, 4, 10, 23, 57, …) performs well in practice; this tool shows Shell’s original halving sequence.
Is it stable?
No.
Why faster than insertion sort?
Large gaps move elements close to their final positions early.