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
| 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) |
| Stable (equal values keep their order) | Yes | Yes |
| In place (no extra array) | Yes | No |
| Needs sorted input | No | No |
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).
| Input | Bubble sort | Merge sort |
|---|---|---|
| Mixed | 40 | 34 |
| Already sorted | 6 | 31 |
| Reversed | 63 | 29 |
| Nearly sorted | 13 | 31 |
| Duplicates | 47 | 32 |
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