Skip to content
SimpleScope

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

PropertyBubble sortMerge sort
TechniqueSwap neighbors that are out of orderDivide and conquer: split, sort each half, merge
Best case timeO(n)O(n log n)
Average case timeO(n²)O(n log n)
Worst case timeO(n²)O(n log n)
Extra spaceO(1)O(n)
Compares values with each otherYesYes
Stable (equal values keep their order)YesYes
In place (no extra array)YesNo

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).

InputBubble sortMerge sort
Mixed4034
Already sorted631
Reversed6329
Nearly sorted1331
Duplicates4732

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