Bubble Sort vs Insertion Sort

Across our 3 test inputs, Insertion Sort needed 85 comparisons in total versus 135 for Bubble Sort.

Head-to-head on identical inputs (comparisons)

InputBubble SortInsertion Sort
Random order4530
Nearly sorted4510
Reversed4545

Complexity

Bubble SortInsertion Sort
Average timeO(n²)O(n²)
Worst timeO(n²)O(n²)
SpaceO(1)O(1)
StableYesYes

When to use Bubble Sort

Educational purposes and very small arrays. Never use in production due to O(n²) worst-case.

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.