Bubble Sort vs Merge Sort
Across our 3 test inputs, Merge Sort needed 59 comparisons in total versus 135 for Bubble Sort. On average Bubble Sort runs in O(n²) time and Merge Sort in O(n log n).
Head-to-head on identical inputs (comparisons)
| Input | Bubble Sort | Merge Sort |
|---|---|---|
| Random order | 45 | 25 |
| Nearly sorted | 45 | 19 |
| Reversed | 45 | 15 |
Complexity
| Bubble Sort | Merge Sort | |
|---|---|---|
| Average time | O(n²) | O(n log n) |
| Worst time | O(n²) | O(n log n) |
| Space | O(1) | O(n) |
| 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 Merge Sort
When stable sorting is strictly required and O(n) extra space is acceptable. Highly predictable performance (always O(n log n)).