Insertion Sort vs Cocktail Shaker Sort

Across our 3 test inputs, Insertion Sort needed 85 comparisons in total versus 101 for Cocktail Shaker Sort.

Head-to-head on identical inputs (comparisons)

InputInsertion SortCocktail Shaker Sort
Random order3039
Nearly sorted1017
Reversed4545

Complexity

Insertion SortCocktail Shaker Sort
Average timeO(n²)O(n²)
Worst timeO(n²)O(n²)
SpaceO(1)O(1)
StableYesYes

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 Cocktail Shaker Sort

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