Skip to content
SimpleScope

Sorting

Bubble sort vs Selection sort

Bubble sort and Selection sort both put an array in order, in different ways. Bubble sort: swap neighbors that are out of order. Selection sort: repeatedly pick the smallest remaining value.

At a glance

PropertyBubble sortSelection sort
TechniqueSwap neighbors that are out of orderRepeatedly pick the smallest remaining value
Best case timeO(n)O(n²)
Average case timeO(n²)O(n²)
Worst case timeO(n²)O(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 data is already close to sorted and you need the swap-neighbours shape for teaching. It is the only one of the two that can detect its own success: a pass with no swap means the array is in order, so it stops. That makes it adaptive where selection sort is not, and it keeps equal values in their original order, which selection sort does not. It is also the only sort here whose best case is O(n) rather than O(n²).

Selection sort: when to choose it

Choose selection sort when writing is the expensive operation. It makes at most n − 1 swaps no matter how scrambled the input is, because each pass finds the minimum and moves it once; bubble sort can swap on nearly every comparison. On flash storage, on a structure where moving an element means rewriting a block, or when the elements are large records, that bounded write count is the whole argument. You give up stability and any benefit from pre-sorted input to get it.

The same inputs, counted

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

InputBubble sortSelection sort
Mixed4029
Already sorted621
Reversed6327
Nearly sorted1323
Duplicates4731

What the numbers show

The two cost curves cross, and both directions of the crossing are visible. On the reversed array bubble sort needs 63 operations and selection sort 27 — more than twice as cheap — because reversing is bubble sort's worst case: every one of its 21 comparisons finds a pair out of order and swaps, and a swap is two writes, so 42 of those 63 operations are memory traffic. Selection sort does the same 21 comparisons but only 3 swaps. On the mixed array the same thing happens more mildly, 40 against 29, and on the duplicates array 47 against 31.

Reverse the input order and the ranking reverses too. On the already-sorted array bubble sort takes 6 operations to selection sort's 21, and on the nearly sorted array 13 to 23. Bubble sort's 6 is six comparisons and zero writes: one clean pass, nothing swapped, stop. Selection sort cannot do that — its comparison count is fixed at 21 for a seven-element array whatever the values are, because scanning for a minimum means looking at every remaining element, every pass, forever.

That fixed cost is the sharpest fact in the table. Selection sort's totals span only 21 to 31 across all five inputs; bubble sort's span 6 to 63, a factor of ten. Selection sort is predictable and bubble sort is adaptive, which is why the "best case" row reads O(n²) for one and O(n) for the other. Neither escapes O(n²) on average, so both answers only matter while n is small.

See them run

More comparisons