Insertion Sort vs Shell Sort

Across our 3 test inputs, Shell Sort needed 79 comparisons in total versus 85 for Insertion Sort. On average Insertion Sort runs in O(n²) time and Shell Sort in Gap-dependent. Insertion Sort is stable; Shell Sort is not.

Head-to-head on identical inputs (comparisons)

InputInsertion SortShell Sort
Random order3029
Nearly sorted1023
Reversed4527

Complexity

Insertion SortShell Sort
Average timeO(n²)Gap-dependent
Worst timeO(n²)O(n²)
SpaceO(1)O(1)
StableYesNo

When to use Insertion Sort

Small arrays or nearly-sorted arrays. O(n) best case for already-sorted input. Used as a subroutine in Timsort.

When to use Shell Sort

Medium-sized arrays and embedded systems where recursion or extra memory is undesirable.