Bubble sort repeatedly swaps adjacent out-of-order elements and runs in O(n^2) time, while quicksort partitions the array around a pivot and recurses, averaging O(n log n). In C, you would use bubble sort only for teaching or tiny arrays and quicksort (or the standard library qsort) for real data.
Both sort in place, but they differ sharply in speed, stability and worst-case behaviour.
Bubble sort makes repeated passes over the array. In each pass it compares neighbouring elements and swaps them if they are in the wrong order, so the largest remaining value "bubbles" to the end. If a full pass makes no swap, the array is sorted and the loop can stop early.
The nested loops give O(n^2) comparisons in the average and worst cases. With the early-exit flag the best case (already sorted input) is O(n). Bubble sort is stable, because equal elements are never swapped, and it needs O(1) extra memory.
Quicksort picks a pivot, partitions the array so smaller values come before it and larger values after it, then sorts the two parts recursively. The version below uses the Lomuto partition scheme with the middle element as pivot, which avoids the worst case on already sorted input.
Quicksort averages O(n log n). Its worst case is O(n^2) when pivots are consistently the smallest or largest value, which a good pivot choice makes rare. It is not stable, and it uses O(log n) stack space on average for the recursion.
| Property | Bubble sort | Quicksort |
|---|---|---|
| Average time | O(n^2) | O(n log n) |
| Worst time | O(n^2) | O(n^2), rare with good pivots |
| Best time | O(n) with early exit | O(n log n) |
| Extra space | O(1) | O(log n) stack on average |
| Stable | Yes | No |
| Typical use | Teaching, tiny arrays | General purpose |
For 100,000 elements bubble sort performs on the order of 10^10 comparisons, while quicksort performs roughly 1.7 million. That gap is why bubble sort is not used in production code.
You rarely need to write quicksort yourself in C. qsort from stdlib.h sorts any array through a comparison function. The C standard does not require it to be quicksort, only that it sorts correctly.
Never write the comparator as a - b, because subtraction can overflow for large or negative values.
qsort, or write quicksort with a median or random pivot.It moves each element only one position per swap, so large values take many passes to reach the end. That yields about n^2 / 2 comparisons.
For anything beyond a handful of elements, yes. On very small arrays the difference is negligible, and an already sorted array favours bubble sort with early exit.
No. Partitioning can reorder equal elements. Use merge sort, or add the original index as a tiebreaker, when stability matters.
Often, but the C standard does not specify the algorithm. Implementations may use introsort or merge sort, so do not rely on a particular behaviour.
Bubble sort is simple, stable and in place but runs in O(n^2). Quicksort averages O(n log n) and is the basis of most practical sorting, though it is unstable and has a rare quadratic worst case. In C, call qsort for production code and keep bubble sort for learning.