Skip to content
SimpleScope

Sorting

Bubble sort vs Quick sort

Bubble sort and Quick sort both put an array in order, in different ways. Bubble sort: swap neighbors that are out of order. Quick sort: divide and conquer: partition around a pivot.

At a glance

PropertyBubble sortQuick sort
TechniqueSwap neighbors that are out of orderDivide and conquer: partition around a pivot
Best case timeO(n)O(n log n)
Average case timeO(n²)O(n log n)
Worst case timeO(n²)O(n²)
Extra spaceO(1)O(log n)
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 for already-ordered or nearly-ordered input, and when a predictable worst case matters more than a good average. That sounds like faint praise until you see what this quick sort does on sorted data: with the Lomuto partition and the last element as pivot, sorted input is its worst case, and it is the exact input on which bubble sort is at its best. Bubble sort is also stable and needs no recursion stack at all.

Quick sort: when to choose it

Choose quick sort for large arrays in unknown or genuinely mixed order, where its O(n log n) average and in-place partitioning win by a wide margin. Partitioning does useful work with each comparison — every element is placed on the correct side of the pivot and never compared with it again — where bubble sort's comparisons move values one position at a time. In production you would also protect the pivot choice (median-of-three, or a random pivot), because the worst case below is not a theoretical curiosity.

The same inputs, counted

These numbers come from running both real implementations: operations (comparisons plus writes, a swap counting as two writes).

InputBubble sortQuick sort
Mixed4031
Already sorted675
Reversed6351
Nearly sorted1365
Duplicates4729

What the numbers show

On mixed and duplicate-heavy data quick sort does what the textbook promises: 31 operations against bubble sort's 40, and 29 against 47. Eleven comparisons is all it needs on the mixed array, fewer than any other comparison sort on this page, because each partition discards a whole side of the array from further consideration.

On the already-sorted array the result is the reverse and much larger: quick sort takes 75 operations, bubble sort 6. That is twelve times the cost, and it is the pivot rule, not bad luck. With the last element as pivot on an ascending array, every element compares as "less than or equal", so the partition point lands at the far end and the recursion peels off one element at a time: 21 comparisons and 54 writes, the O(n²) worst case the table's own "worst case time" row records. The nearly sorted array is barely better, 65 against 13.

Even on the reversed array, bubble sort's own worst case at 63 operations, quick sort only wins by 51 to 63 — a sixth, not an order of magnitude. The useful reading is that quick sort's reputation is an average-case reputation that depends entirely on partitions being balanced, and that on seven elements no comparison sort can separate itself by much. Choose quick sort for scale, and fix the pivot before trusting it on data that might arrive sorted.

See them run

More comparisons