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)
| Input | Bubble Sort | Insertion Sort |
|---|---|---|
| Random order | 45 | 30 |
| Nearly sorted | 45 | 10 |
| Reversed | 45 | 45 |
Complexity
| Bubble Sort | Insertion Sort | |
|---|---|---|
| Average time | O(n²) | O(n²) |
| Worst time | O(n²) | O(n²) |
| Space | O(1) | O(1) |
| Stable | Yes | Yes |
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.