Skip to content
SimpleScope

Sorting

Bubble sort vs Insertion sort

Bubble sort and Insertion sort both put an array in order, in different ways. Bubble sort: swap neighbors that are out of order. Insertion sort: insert each value into a sorted prefix.

At a glance

PropertyBubble sortInsertion sort
TechniqueSwap neighbors that are out of orderInsert each value into a sorted prefix
Best case timeO(n)O(n)
Average case timeO(n²)O(n²)
Worst case timeO(n²)O(n²)
Extra spaceO(1)O(1)
Compares values with each otherYesYes
Stable (equal values keep their order)YesYes
In place (no extra array)YesYes

When to choose each

Bubble sort: when to choose it

Choose bubble sort almost only to explain sorting, or when the array is already in order and you want the cheapest possible proof of it. Its single advantage over insertion sort is that a verification pass is free: n − 1 comparisons and not one write. Insertion sort, in this canonical form, still writes every key back into place even when the key does not move. Everywhere else bubble sort is insertion sort with extra swapping, and the counts below say so.

Insertion sort: when to choose it

Choose insertion sort as the default small-array sort. Both are stable, in place, O(1) extra space and O(n²) in the worst case, but insertion sort stops comparing as soon as it meets a value smaller than its key, while bubble sort rescans the whole unsorted prefix on every pass. That is why insertion sort is what real libraries fall back to below a size threshold, and why it is the sort to use on data arriving one element at a time — it keeps a correct sorted prefix at every moment.

The same inputs, counted

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

InputBubble sortInsertion sort
Mixed4030
Already sorted612
Reversed6348
Nearly sorted1314
Duplicates4736

What the numbers show

On disordered input insertion sort is clearly cheaper: 30 operations against 40 on the mixed array, 48 against 63 on the reversed one, 36 against 47 on the duplicates. The reversed case explains the mechanism. Both make 21 comparisons there — the maximum for seven elements — but bubble sort turns every one of them into a swap, 42 writes, while insertion sort shifts and places, 27 writes. A shift moves one value; a swap moves two.

On ordered input the ranking flips, narrowly. Already sorted: 6 against 12. Nearly sorted: 13 against 14, a difference of one operation. Both are adaptive and both have an O(n) best case, but insertion sort's best case is six comparisons plus six writes, because the canonical version assigns the key into its slot unconditionally. Bubble sort's is six comparisons and nothing else.

Read that as the real tradeoff rather than a win: bubble sort beats insertion sort only on input that is already sorted, by writes it avoids making, and loses by 25 to 50 percent everywhere else. Add that insertion sort's comparison count drops with the disorder in the input — 14 operations on the nearly sorted array against 48 on the reversed one — and there is no input size at which bubble sort is the better production choice.

See them run

More comparisons