Bubble Sort vs Heap Sort
Across our 3 test inputs, Heap Sort needed 112 comparisons in total versus 135 for Bubble Sort. On average Bubble Sort runs in O(n²) time and Heap Sort in O(n log n). Bubble Sort is stable; Heap Sort is not.
Head-to-head on identical inputs (comparisons)
| Input | Bubble Sort | Heap Sort |
|---|---|---|
| Random order | 45 | 36 |
| Nearly sorted | 45 | 41 |
| Reversed | 45 | 35 |
Complexity
| Bubble Sort | Heap Sort | |
|---|---|---|
| Average time | O(n²) | O(n log n) |
| Worst time | O(n²) | O(n log n) |
| Space | O(1) | O(1) |
| Stable | Yes | No |
When to use Bubble Sort
Educational purposes and very small arrays. Never use in production due to O(n²) worst-case.
When to use Heap Sort
When you need guaranteed O(n log n) with O(1) extra memory — e.g. embedded or real-time systems.