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
| Property | Bubble sort | Selection sort |
|---|---|---|
| Technique | Swap neighbors that are out of order | Repeatedly pick the smallest remaining value |
| Best case time | O(n) | O(n²) |
| Average case time | O(n²) | O(n²) |
| Worst case time | O(n²) | O(n²) |
| Extra space | O(1) | O(1) |
| 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 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).
| Input | Bubble sort | Selection sort |
|---|---|---|
| Mixed | 40 | 29 |
| Already sorted | 6 | 21 |
| Reversed | 63 | 27 |
| Nearly sorted | 13 | 23 |
| Duplicates | 47 | 31 |
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
- Bubble sort vs Insertion sort
- Bubble sort vs Merge sort
- Bubble sort vs Quick 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