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)

InputSelection SortHeap Sort
Random order4536
Nearly sorted4541
Reversed4535

Complexity

Selection SortHeap Sort
Average timeO(n²)O(n log n)
Worst timeO(n²)O(n log n)
SpaceO(1)O(1)
StableNoNo

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.