Skip to content
StackDecode
SortingAnimated

Shell Sort

Insertion sort over shrinking gaps.

Time

About O(n^1.3) in practice

Extra space

O(1)

Use it for

Medium arrays, simple code, no recursion.

How it works

  1. Insertion sort is slow because it moves items one step at a time. Shell sort first sorts items that are far apart.
  2. Pick a gap (start at n/2). Do an insertion sort on every gap-th element.
  3. Halve the gap and repeat.
  4. When the gap is 1 it is a normal insertion sort, but the list is already nearly sorted, so it finishes fast.

Watch it run

Loading animation…