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
| Property | Bubble sort | Insertion sort |
|---|---|---|
| Technique | Swap neighbors that are out of order | Insert each value into a sorted prefix |
| Best case time | O(n) | O(n) |
| Average case time | O(n²) | O(n²) |
| Worst case time | O(n²) | O(n²) |
| Extra space | O(1) | O(1) |
| Compares values with each other | Yes | Yes |
| Stable (equal values keep their order) | Yes | Yes |
| In place (no extra array) | Yes | Yes |
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).
| Input | Bubble sort | Insertion sort |
|---|---|---|
| Mixed | 40 | 30 |
| Already sorted | 6 | 12 |
| Reversed | 63 | 48 |
| Nearly sorted | 13 | 14 |
| Duplicates | 47 | 36 |
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
- Bubble sort vs Selection sort
- Bubble sort vs Merge sort
- Bubble sort vs Quick sort
- Bubble sort vs Heap sort
- Bubble sort vs Counting sort
- Bubble sort vs Radix sort
- Selection sort vs Insertion sort
- Selection sort vs Merge sort
- Selection sort vs Quick sort
- Selection sort vs Heap sort
- Selection sort vs Counting sort
- Selection sort vs Radix sort
- Insertion sort vs Merge sort
- Insertion sort vs Quick sort
- Insertion sort vs Heap sort
- Insertion sort vs Counting sort
- Insertion sort vs Radix sort
- Merge sort vs Quick sort
- Merge sort vs Heap sort
- Merge sort vs Counting sort
- Merge sort vs Radix sort
- Quick sort vs Heap sort
- Quick sort vs Counting sort
- Quick sort vs Radix sort
- Heap sort vs Counting sort
- Heap sort vs Radix sort
- Counting sort vs Radix sort