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. In the worst case merge sort takes O(n log n) while bubble sort takes O(n²), so merge sort scales better on large inputs.

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)
Stable (equal values keep their order)YesYes
In place (no extra array)YesNo
Needs sorted inputNoNo

When to choose each

Choose bubble sort when

you want the simplest sort to explain, on tiny or almost sorted data.

Avoid it when

the data is large.

Choose merge sort when

you need a guaranteed O(n log n) time or a stable sort.

Avoid it when

memory is tight.

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

See them run

More comparisons