Comparisons

Bubble Sort vs Insertion Sort vs Selection Sort

All three simple sorts are O(n²), but one is clearly best. Bubble vs Insertion vs Selection Sort on comparisons, swaps, stability and real use.

Aman Jaiman
8 min read
On this page +
  1. In plain English
  2. How they differ
  3. Why Insertion Sort wins
  4. Insertion Sort vs Selection Sort
  5. The verdict on all three
  6. Frequently asked questions

Bubble Sort vs Insertion Sort is a classic beginner comparison. Both are simple, stable, in-place, and O(n²), so they look interchangeable on paper. In practice, Insertion Sort is meaningfully faster — and understanding why is a great lesson in reading beyond Big O.

How they differ #

Bubble Sort repeatedly swaps adjacent out-of-order pairs, performing many swaps per pass. Insertion Sort shifts elements and inserts each one once, doing far fewer writes. Both reach O(n) on already-sorted data (Bubble needs the swapped-flag optimization), but Insertion Sort's smaller constant factors win on typical input.

Why Insertion Sort wins #

For the same number of comparisons, Insertion Sort moves data less and has a tighter loop, so it is roughly 2x faster in practice. This is why hybrid sorts like Tim Sort and Introsort use Insertion Sort — never Bubble Sort — for small subarrays. See the Insertion Sort guide for details.

Insertion Sort vs Selection Sort #

Insertion Sort and Selection Sort are the closer contest, because they optimise different things.

Insertion SortSelection Sort
ComparisonsO(n²) average, O(n) if sortedAlways ~n²/2
Writes / swapsO(n²) shiftsExactly n−1 swaps
AdaptiveYes — dramaticallyNo
StableYesNo
Early exit possibleYesNo

Insertion Sort wins on almost every axis that normally matters. It is stable, it is adaptive — on nearly-sorted input it approaches O(n) and finishes almost instantly — and it can stop early. Selection Sort is stubbornly indifferent to its input: it performs the same full scan whether the array is random, sorted or reversed, because it must examine every remaining element to be sure it has found the minimum.

So why does Selection Sort survive at all? Because of the one row where it wins decisively: it performs exactly n−1 swaps, the theoretical minimum. Insertion Sort may shift elements O(n²) times. If reads are cheap but writes are expensive or damaging — flash memory with limited erase cycles, or a structure where moving an element is costly — minimising writes can matter more than minimising comparisons. That is a narrow niche, but it is a real one.

The visualiser makes this vivid. Run both on the same array and watch the two counters separately: Selection Sort's comparison count climbs relentlessly while its swap count barely moves, and Insertion Sort does the opposite.

The verdict on all three #

Ranked for general use, it is not close:

  • Insertion Sort — the clear winner. Stable, adaptive, simple, and genuinely used in production as the base case inside Tim Sort and introsort once a partition gets small enough.
  • Selection Sort — second, and only because of its minimal write count. Otherwise strictly worse.
  • Bubble Sort — last. It has no property that another algorithm does not do better. Its enduring value is entirely pedagogical: it is the easiest sorting algorithm to explain and to watch.

An important caveat about all three: quadratic does not mean useless. For arrays of roughly 10 to 20 elements, Insertion Sort typically beats Merge Sort and Quick Sort outright, because the recursion and bookkeeping in an O(n log n) algorithm cost more than the handful of extra comparisons. That is not a curiosity — it is why Tim Sort and introsort both fall back to Insertion Sort for small runs.

Found this useful?

Share it with someone who is learning this too.

Questions

Frequently asked questions

Is Insertion Sort faster than Bubble Sort? +
Yes. Although both are O(n²), Insertion Sort does fewer writes and has smaller constant factors, making it about twice as fast in practice. It is the one hybrid sorts use internally.
Are both Bubble Sort and Insertion Sort stable? +
Yes, both are stable and in-place with O(1) space. The practical difference is speed, where Insertion Sort wins.
Which is better, Insertion Sort or Selection Sort? +
Insertion Sort, for almost every purpose. It is stable, it adapts to nearly-sorted input (approaching O(n) where Selection Sort stays at O(n²)), and it can exit early. Selection Sort's single advantage is that it performs exactly n−1 swaps — the minimum possible — which only matters when writing data is far more expensive than reading it.
Why does Selection Sort make fewer swaps? +
Because it commits to a position before it moves anything. Each pass scans the entire unsorted region, identifies the minimum, and then performs exactly one swap to put that value in its final place. Insertion Sort instead shifts every larger element one slot to the right to open a gap for the key, so a single insertion can cause many writes. Selection Sort trades a lot of looking for very little moving.

See it in motion

Watch this algorithm and nine others run step by step, with live pseudocode and comparison counters.

Launch the visualiser
Aman Jaiman
Written by
Aman Jaiman

Software engineer at a stealth-stage startup, and previously a front-end engineer for around a year and a half.

Keep reading

Related guides

Link copied