Selection Sort vs Heap Sort
Across our 3 test inputs, Heap Sort needed 112 comparisons in total versus 135 for Selection Sort. On average Selection Sort runs in O(n²) time and Heap Sort in O(n log n).
Head-to-head on identical inputs (comparisons)
| Input | Selection Sort | Heap Sort |
|---|---|---|
| Random order | 45 | 36 |
| Nearly sorted | 45 | 41 |
| Reversed | 45 | 35 |
Complexity
| Selection 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 | No | No |
When to use Selection Sort
When memory writes are expensive (minimizes swaps). Otherwise, insertion sort is generally preferred.
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.