How does bubble sort compare to selection sort?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Bubble sort and selection sort are both in-place quadratic sorting algorithms often taught early in programming courses. They look similar in Big O notation, but their operational behavior is different, especially for swap count, stability, and nearly sorted inputs. Understanding these differences helps explain when each algorithm is acceptable and why modern libraries use better alternatives.
Bubble Sort Mechanics
Bubble sort repeatedly scans adjacent pairs and swaps out-of-order neighbors. After each pass, the largest remaining element “bubbles” to the right end.
The swapped flag provides an early exit. If no swap happens in a pass, array is already sorted.
Selection Sort Mechanics
Selection sort partitions the array into sorted prefix and unsorted suffix. Each outer pass finds minimum value in unsorted part and places it at next sorted position.
Selection sort does fewer swaps than bubble sort, but still scans most of the remaining array every pass.
Complexity Comparison
Both algorithms have quadratic comparison complexity in average and worst cases.
- Bubble sort worst-case comparisons: on the order of
nsquared. - Selection sort worst-case comparisons: on the order of
nsquared.
Key differences:
- Bubble sort with early exit can be close to linear on already sorted data.
- Selection sort remains quadratic even when input is already sorted.
So Big O alone misses useful behavioral detail.
Swap Count and Write Cost
Swap behavior is often the biggest practical difference.
- Bubble sort can perform many swaps, potentially almost every inversion.
- Selection sort performs at most one swap per outer loop.
If writes are expensive, selection sort can outperform bubble sort despite similar comparison count. This matters in certain embedded or write-sensitive environments.
Stability
Stability means equal keys preserve original order after sorting.
- Bubble sort is stable in standard form when swapping only on strict greater-than.
- Selection sort is generally unstable due to long-distance swaps.
If sorting records by multiple keys across multiple passes, stability can be important.
Example record list:
A stable algorithm keeps A before C among equal key value 2.
Practical Performance on Small Arrays
On tiny arrays, both can be acceptable for teaching or simple scripts. For production sorting, almost always use built-in language sort:
- Python uses Timsort.
- Java uses optimized hybrid strategies.
- C plus plus typically uses introspective sorting variants.
These algorithms provide near-linearithmic behavior and strong engineering optimizations.
When You Might Still Use Them
Reasonable use cases today:
- Teaching loop invariants and algorithm reasoning.
- Coding interviews focused on basics.
- Very small fixed-size lists where clarity matters more than speed.
In all other cases, built-in sort is safer and faster.
Small Benchmark Skeleton
If you want to compare empirically in Python, measure with random arrays.
Keep dataset and environment constant for fair comparison.
Common Pitfalls
A common pitfall is assuming equal Big O means equal runtime under real data conditions. Another issue is using plain bubble sort without early-exit optimization, which overstates work on nearly sorted input. Teams also overlook stability requirements and pick selection sort where stable order is needed. Benchmarking with tiny arrays only can produce misleading conclusions. Finally, implementing custom sort in production where built-in sort exists usually increases risk without clear benefit.
Summary
- Bubble and selection sort are both in-place quadratic algorithms.
- Bubble sort can be adaptive with early exit and is usually stable.
- Selection sort uses fewer swaps but is typically unstable.
- Both are mainly educational for modern software engineering.
- For real workloads, prefer standard library sorting implementations.
- Use algorithm comparisons to learn tradeoffs, not to justify replacing proven built-in sorts.
Related reading
- How does Dijkstra's Algorithm and A-Star compare?
- How does Dijkstra's self-stabilizing algorithm work?
- How does Elasticsearch recover from a quorum that is not unanimous
- How does finding a cycle start node in a cycle linked list work?
- How does Firefox's 'awesome' bar match strings?
- How does Google recognizes adult content with safesearch?
- How does Hibernate's batch-fetching algorithm work?
- How does LCP help in finding the number of occurrences of a pattern?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.