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. In the worst case both take O(n²), so the choice comes down to the other differences below.

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)
Stable (equal values keep their order)YesYes
In place (no extra array)YesYes
Needs sorted inputNoNo

When to choose each

Choose bubble sort when

you want the simplest sort to explain, on tiny or almost sorted data.

Avoid it when

the data is large.

Choose insertion sort when

the array is small or nearly sorted.

Avoid it when

the array is large and in random order.

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

See them run

More comparisons