Sorting
Bubble sort vs Merge sort
Bubble sort and Merge sort both put an array in order, in different ways. Bubble sort: swap neighbors that are out of order. Merge sort: divide and conquer: split, sort each half, merge.
At a glance
| Property | Bubble sort | Merge sort |
|---|---|---|
| Technique | Swap neighbors that are out of order | Divide and conquer: split, sort each half, merge |
| Best case time | O(n) | O(n log n) |
| Average case time | O(n²) | O(n log n) |
| Worst case time | O(n²) | O(n log n) |
| Extra space | O(1) | O(n) |
| Compares values with each other | Yes | Yes |
| Stable (equal values keep their order) | Yes | Yes |
| In place (no extra array) | Yes | No |
When to choose each
Bubble sort: when to choose it
Choose bubble sort when n is genuinely tiny or the array is nearly in order, and when you cannot spare a second array. It needs O(1) extra space against merge sort's O(n), and it short-circuits: on input that is already sorted it is one pass and done. On seven elements those two properties are enough to beat an asymptotically better algorithm, which is the useful lesson here — O(n²) versus O(n log n) is a statement about growth, not about small inputs.
Merge sort: when to choose it
Choose merge sort when the array is large or the input order is unknown, and when you need a guarantee. It is O(n log n) in the best, average and worst case alike; bubble sort's worst case is quadratic and arrives on exactly the input you would least expect to be hard. Merge sort is also stable, and it is the sort that generalises to data that does not fit in memory, because merging reads both halves in order. The cost is an extra array the size of the input.
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 | Merge sort |
|---|---|---|
| Mixed | 40 | 34 |
| Already sorted | 6 | 31 |
| Reversed | 63 | 29 |
| Nearly sorted | 13 | 31 |
| Duplicates | 47 | 32 |
What the numbers show
Merge sort's cost barely moves: 31, 29, 31, 34 and 32 operations across the five cases, a spread of five. Twenty of those operations are writes in every single case, because merge sort copies each element back out of its two halves exactly once per level of recursion regardless of the values; only the comparison count varies, from 9 to 14. Bubble sort's totals span 6 to 63.
So the comparison is between a flat line and a steep one, and at n = 7 they cross twice. Bubble sort wins big on the sorted array, 6 against 31, and on the nearly sorted one, 13 against 31: merge sort pays its full recursion and 20 writes to discover that there was nothing to do. Merge sort wins on the reversed array, 29 against 63, and more modestly on mixed, 34 against 40, and duplicates, 32 against 47.
Interestingly the reversed array is merge sort's cheapest input, 29, while it is bubble sort's most expensive. Merging two descending-then-reversed halves exhausts one side early, so fewer comparisons are needed; the writes are fixed. Scale the arrays up and the crossing points stop mattering: at n = 1,000, n²/2 is about 500,000 and n log₂ n about 10,000, and no constant factor or early exit closes a 50-fold gap.
See them run
More comparisons
- Bubble sort vs Selection sort
- Bubble sort vs Insertion 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