Skip to content
SimpleScope

Sorting

Selection sort vs Heap sort

Selection sort and Heap sort both put an array in order, in different ways. Selection sort: repeatedly pick the smallest remaining value. Heap sort: build a max-heap in the array, then repeatedly move the largest value to the end.

At a glance

PropertySelection sortHeap sort
TechniqueRepeatedly pick the smallest remaining valueBuild a max-heap in the array, then repeatedly move the largest value to the end
Best case timeO(n²)O(n log n)
Average case timeO(n²)O(n log n)
Worst case timeO(n²)O(n log n)
Extra spaceO(1)O(1)
Compares values with each otherYesYes
Stable (equal values keep their order)NoNo
In place (no extra array)YesYes

When to choose each

Selection sort: when to choose it

Choose selection sort on small arrays, and when you want the simplest possible code with a hard bound on writes: at most n − 1 swaps, regardless of input. The two algorithms answer the same question — repeatedly take the largest or smallest remaining value — so the comparison is really about the data structure used to find it. Selection sort scans linearly, which costs more comparisons but no setup; on a handful of elements the setup is what dominates.

Heap sort: when to choose it

Choose heap sort the moment the array stops being small. It replaces selection sort's linear scan for the extreme value with a max-heap, which turns each selection from O(n) into O(log n) and the whole sort from O(n²) into a guaranteed O(n log n) — in the worst case, not just on average. It keeps selection sort's O(1) extra space, because the heap lives in the array itself. Both are unstable, so stability is not the deciding factor between them.

The same inputs, counted

These numbers come from running both real implementations: operations (comparisons plus writes, a swap counting as two writes).

InputSelection sortHeap sort
Mixed2943
Already sorted2153
Reversed2740
Nearly sorted2350
Duplicates3136

What the numbers show

At seven elements selection sort wins all five cases: 29 operations against 43 on the mixed array, 21 against 53 on the already-sorted one, 27 against 40 on the reversed one, 23 against 50 on the nearly sorted one, and 31 against 36 on the duplicates. The heap is a fixed investment, and seven elements do not repay it.

The comparison counts show why heap sort is not simply losing. On the duplicates array it makes 16 comparisons against selection sort's 21, and on the reversed array 18 against 21 — the heap really does find each maximum with fewer comparisons than a linear scan. What it loses on is writes: swapping the root to the end and then sifting it back down moves a value repeatedly, 20 to 32 writes depending on the input, against selection sort's 0 to 10.

The two curves are n(n − 1)/2 against roughly 2n log₂ n, and they cross early. At n = 7 that is 21 against about 39; at n = 50, 1,225 against about 564; at n = 1,000, roughly half a million against about 20,000. Heap sort's worst case on this page, 53 on the already-sorted array, is the right thing to notice: it has no best case and no adaptivity, but it also has no bad case, which is the property selection sort cannot offer at scale.

See them run

More comparisons