Sorting
Bubble sort vs Heap sort
Bubble sort and Heap sort both put an array in order, in different ways. Bubble sort: swap neighbors that are out of order. Heap sort: build a max-heap in the array, then repeatedly move the largest value to the end.
At a glance
| Property | Bubble sort | Heap sort |
|---|---|---|
| Technique | Swap neighbors that are out of order | Build a max-heap in the array, then repeatedly move the largest value to the end |
| 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(1) |
| Compares values with each other | Yes | Yes |
| Stable (equal values keep their order) | Yes | No |
| In place (no extra array) | Yes | Yes |
When to choose each
Bubble sort: when to choose it
Choose bubble sort when the array is small or nearly ordered and you want stability. Heap sort has no best case worth the name: building the heap and sifting values back down costs the same whatever order the input arrived in, so an already-sorted array gets no discount at all. Bubble sort also preserves the order of equal values, which heap sort scrambles, and it never moves a value more than one position at a time, which makes it the one to reach for when you are explaining what a sort is.
Heap sort: when to choose it
Choose heap sort when the array is large, memory is tight and you need a guarantee. It is O(n log n) in every case with O(1) extra space — the only sort on this page with both — because the heap lives inside the array being sorted. Bubble sort offers nothing comparable: its average and worst cases are quadratic, so the gap grows without bound. Heap sort's price is that it is unstable and that its access pattern jumps between parents and children instead of scanning neighbours.
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 | Heap sort |
|---|---|---|
| Mixed | 40 | 43 |
| Already sorted | 6 | 53 |
| Reversed | 63 | 40 |
| Nearly sorted | 13 | 50 |
| Duplicates | 47 | 36 |
What the numbers show
Heap sort's totals cluster between 36 and 53 operations, and the shape of that range is the point: its most expensive input is the array that was already sorted. Building a max-heap from ascending values moves nearly everything, and then all seven extractions have to sift a value from the root all the way back down. Bubble sort finishes that same array in 6 operations — six comparisons, no writes, one pass.
The nearly sorted array repeats it, 13 against 50, and even the mixed array goes narrowly to bubble sort, 40 against 43. Heap sort wins where disorder is real: 40 against 63 on the reversed array, a 36 percent saving, and 36 against 47 on the duplicates.
None of that survives scale, and the table's complexity rows say why: O(n log n) against O(n²) in the worst case, with O(1) space for both. At seven elements log₂ n is under 3, so heap sort's asymptotic advantage is worth less than the constant cost of building and repairing the heap. By a few hundred elements the picture has inverted permanently, and bubble sort's adaptivity on sorted input is no longer worth anything.
See them run
More comparisons
- Bubble sort vs Selection sort
- Bubble sort vs Insertion sort
- Bubble sort vs Merge sort
- Bubble sort vs Quick 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