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.
On this page +
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 Sort | Selection Sort | |
|---|---|---|
| Comparisons | O(n²) average, O(n) if sorted | Always ~n²/2 |
| Writes / swaps | O(n²) shifts | Exactly n−1 swaps |
| Adaptive | Yes — dramatically | No |
| Stable | Yes | No |
| Early exit possible | Yes | No |
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.
Frequently asked questions
Is Insertion Sort faster than Bubble Sort? +
Are both Bubble Sort and Insertion Sort stable? +
Which is better, Insertion Sort or Selection Sort? +
Why does Selection Sort make fewer swaps? +
See it in motion
Watch this algorithm and nine others run step by step, with live pseudocode and comparison counters.
Launch the visualiser
Software engineer at a stealth-stage startup, and previously a front-end engineer for around a year and a half.