Sorting
Insertion sort vs Heap sort
Insertion sort and Heap sort both put an array in order, in different ways. Insertion sort: insert each value into a sorted prefix. Heap sort: build a max-heap in the array, then repeatedly move the largest value to the end.
At a glance
| Property | Insertion sort | Heap sort |
|---|---|---|
| Technique | Insert each value into a sorted prefix | 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) | Yes | No |
| In place (no extra array) | Yes | Yes |
When to choose each
Insertion sort: when to choose it
Choose insertion sort for small arrays, for nearly sorted data, and when stability matters. Heap sort is not adaptive at all — order already present in the input buys it nothing, and its most expensive case on this page is an array that was already sorted — and it is not stable, so equal records come out in an arbitrary order. Insertion sort is also the gentler access pattern: it touches neighbours, where sifting a value through a heap jumps between indexes i, 2i + 1 and 2i + 2.
Heap sort: when to choose it
Choose heap sort when the array is large, memory is tight, and the worst case has to be bounded. It is O(n log n) in every case with O(1) extra space, so no input makes it slow and none makes it allocate; insertion sort's worst case is quadratic and arrives on reversed input. If you need a guarantee without merge sort's extra array, heap sort is the sort that provides it, at the cost of stability and of any benefit from pre-sorted data.
The same inputs, counted
These numbers come from running both real implementations: operations (comparisons plus writes, a swap counting as two writes).
| Input | Insertion sort | Heap sort |
|---|---|---|
| Mixed | 30 | 43 |
| Already sorted | 12 | 53 |
| Reversed | 48 | 40 |
| Nearly sorted | 14 | 50 |
| Duplicates | 36 | 36 |
What the numbers show
Insertion sort wins three cases, ties one, and loses one. The already-sorted array is the widest gap: 12 operations against 53, a factor of more than four, and in the direction the complexity rows would not predict. Building a max-heap out of ascending values moves nearly every element, and then each of the six extractions sifts a value from the root back down. The nearly sorted array repeats it, 14 against 50, and the mixed array goes 30 against 43.
Heap sort wins the reversed array, 40 against 48, which is insertion sort's worst case: 21 comparisons and 27 writes, every key shifted past the whole prefix. The duplicates array is an exact tie at 36 and 36 operations — reached completely differently, 17 comparisons and 19 writes for insertion sort against 16 and 20 for heap sort.
Heap sort's five totals span 36 to 53, a spread of 17, while insertion sort's span 12 to 48, a spread of 36. That is the real distinction: one is flat and bounded, the other is adaptive with a quadratic ceiling. At n = 7, 2n log₂ n is about 39 and n²/4 about 12, so insertion sort deserves to win here; at n = 1,000 it is about 20,000 against about 250,000 and the guarantee takes over for good.
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 Heap sort
- Selection sort vs Counting sort
- Selection sort vs Radix sort
- Insertion sort vs Merge sort
- Insertion sort vs Quick 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