Skip to content
SimpleScope

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

PropertyInsertion sortHeap sort
TechniqueInsert each value into a sorted prefixBuild 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)YesNo
In place (no extra array)YesYes

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).

InputInsertion sortHeap sort
Mixed3043
Already sorted1253
Reversed4840
Nearly sorted1450
Duplicates3636

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