sort.cny.shStart a race
Back to library
06 / 18Algorithm

Shell Sort

Insertion sort on shrinking gaps — breaks up the O(n²) wall.

StableIn-placeUnstable
BestO(n log n)
Average≈O(n^1.3)
WorstO(n²)
01How it works

Run insertion sort on elements spaced a large "gap" apart, then shrink the gap and repeat. Early passes move items long distances cheaply, and the final gap-1 pass is a nearly-sorted insertion sort — which is the case where insertion sort excels.

02Best for

Medium arrays (thousands of elements) on memory-constrained systems. In-place, no recursion, no auxiliary buffer — a solid middle ground when you can't afford mergesort's O(n) working space.

03Avoid when

You need a tight theoretical worst-case bound. Performance depends heavily on the gap sequence, and the analysis is famously messy; reach for heapsort when guarantees matter.