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
| Property | Bubble sort | Quick sort |
|---|---|---|
| Technique | Swap neighbors that are out of order | Divide and conquer: partition around a pivot |
| 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²) |
| Extra space | O(1) | O(log n) |
| 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 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).
| Input | Bubble sort | Quick sort |
|---|---|---|
| Mixed | 40 | 31 |
| Already sorted | 6 | 75 |
| Reversed | 63 | 51 |
| Nearly sorted | 13 | 65 |
| Duplicates | 47 | 29 |
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
- Bubble sort vs Selection sort
- Bubble sort vs Insertion sort
- Bubble sort vs Merge 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