Skip to content
SimpleScope

Sorting

Insertion sort vs Merge sort

Insertion sort and Merge sort both put an array in order, in different ways. Insertion sort: insert each value into a sorted prefix. Merge sort: divide and conquer: split, sort each half, merge.

At a glance

PropertyInsertion sortMerge sort
TechniqueInsert each value into a sorted prefixDivide and conquer: split, sort each half, merge
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(n)
Compares values with each otherYesYes
Stable (equal values keep their order)YesYes
In place (no extra array)YesNo

When to choose each

Insertion sort: when to choose it

Choose insertion sort for small or nearly sorted arrays, and when you cannot allocate. It is O(1) extra space against merge sort's O(n), it is adaptive — its comparison count falls towards n as the input gets closer to sorted — and it is online, so it can absorb elements as they arrive. Both sorts are stable, so that is not the deciding factor; the decision is really between adaptivity with a quadratic ceiling and a flat guarantee that costs memory.

Merge sort: when to choose it

Choose merge sort when the array is large or its order is unknown. O(n log n) in the best, average and worst case means no input can make it slow, where insertion sort degrades to O(n²) on anything badly out of order. It is also the practical choice for data too big for memory and for linked structures without random access, since merging only ever reads forwards. In exchange it allocates a copy of the data and writes every element back on every level of the recursion.

The same inputs, counted

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

InputInsertion sortMerge sort
Mixed3034
Already sorted1231
Reversed4829
Nearly sorted1431
Duplicates3632

What the numbers show

Where the input has order to exploit, insertion sort wins clearly: 12 operations against 31 on the already-sorted array and 14 against 31 on the nearly sorted one, roughly half the cost in both cases. Merge sort has to split and merge regardless, and its 20 writes per run are paid in full even when every value is already in place. On the mixed array insertion sort still edges ahead, 30 against 34.

Where the input is genuinely disordered, merge sort wins: 29 against 48 on the reversed array and 32 against 36 on the duplicates. The reversed array shows both mechanisms at once. Insertion sort hits its worst case — 21 comparisons and 27 writes, because every new key must be shifted past the entire prefix — while merge sort hits its cheapest input of the five at 9 comparisons, since each merge exhausts one side early.

Merge sort's whole range is 29 to 34, a spread of five operations; insertion sort's is 12 to 48, a spread of 36. That contrast is the comparison. At n = 7, n²/4 (insertion sort's average) and n log₂ n are both about 20, so the inputs decide; by n = 1,000 it is about 250,000 against about 10,000 and the guarantee wins outright. This is exactly why production sorts are hybrids: merge or quick sort for the recursion, insertion sort for the small runs at the bottom.

See them run

More comparisons