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
| Property | Selection sort | Heap sort |
|---|---|---|
| Technique | Repeatedly pick the smallest remaining value | Build a max-heap in the array, then repeatedly move the largest value to the end |
| Best case time | O(n²) | O(n log n) |
| Average case time | O(n²) | O(n log n) |
| Worst case time | O(n²) | O(n log n) |
| Extra space | O(1) | O(1) |
| Compares values with each other | Yes | Yes |
| Stable (equal values keep their order) | No | No |
| In place (no extra array) | Yes | Yes |
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).
| Input | Selection sort | Heap sort |
|---|---|---|
| Mixed | 29 | 43 |
| Already sorted | 21 | 53 |
| Reversed | 27 | 40 |
| Nearly sorted | 23 | 50 |
| Duplicates | 31 | 36 |
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
- Bubble sort vs Selection sort
- Bubble sort vs Insertion sort
- Bubble sort vs Merge sort
- Bubble sort vs Quick sort
- Bubble sort vs Heap sort
- Bubble sort vs Counting sort
- Bubble sort vs Radix sort
- Selection sort vs Insertion sort
- Selection sort vs Merge sort
- Selection sort vs Quick sort
- Selection sort vs Counting sort
- Selection sort vs Radix sort
- Insertion sort vs Merge sort
- Insertion sort vs Quick sort
- Insertion sort vs Heap sort
- Insertion sort vs Counting sort
- Insertion sort vs Radix sort
- Merge sort vs Quick sort
- Merge sort vs Heap sort
- Merge sort vs Counting sort
- Merge sort vs Radix sort
- Quick sort vs Heap sort
- Quick sort vs Counting sort
- Quick sort vs Radix sort
- Heap sort vs Counting sort
- Heap sort vs Radix sort
- Counting sort vs Radix sort