Cocktail Shaker Sort vs Shell Sort

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

Head-to-head on identical inputs (comparisons)

InputCocktail Shaker SortShell Sort
Random order3929
Nearly sorted1723
Reversed4527

Complexity

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

When to use Cocktail Shaker Sort

Teaching, or small arrays where small values sit near the end (where plain Bubble Sort is slow).

When to use Shell Sort

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