Skip to content
SimpleScope

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

PropertyBubble sortHeap sort
TechniqueSwap neighbors that are out of orderBuild a max-heap in the array, then repeatedly move the largest value to the end
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(1)
Compares values with each otherYesYes
Stable (equal values keep their order)YesNo
In place (no extra array)YesYes

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

InputBubble sortHeap sort
Mixed4043
Already sorted653
Reversed6340
Nearly sorted1350
Duplicates4736

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