Bubble Sort Formula
Bubble sort is a simple sorting algorithm that repeatedly walks through the list, compares each pair of adjacent elements, and swaps them if they are in the wrong order.
The Formula
Ask yourself: Is a list sorted by repeatedly swapping adjacent items that are out of order?
Quick example
What this formula means
A simple sorting algorithm that repeatedly walks through the list, compares each pair of adjacent elements, and swaps them if they are in the wrong order. This process repeats until no more swaps are needed, meaning the list is fully sorted.
Bubble sort is like tidying books by height on a shelf when you may only swap two neighboring books. Walking left to right, you swap whenever a taller book stands left of a shorter one, so the tallest book is carried to the right end in a single sweep. A short book stuck at the far right moves only one place per sweep, which is why the method is slow.
Formal view
Worked examples
Example 1
mediumShow solution
Answer
First step
See the full worked solution + why-it-works coaching
SetupKey insightWhy it worksCommon pitfallConnection
Example 2
mediumExample 3
mediumCommon mistakes
- Not optimizing with an early exit—if a pass makes no swaps, the list is already sorted and you can stop
- Comparing elements beyond the sorted portion at the end of the array, wasting comparisons
- Using bubble sort for large datasets when algorithms are available and dramatically faster
Why this formula matters
Bubble sort is easy to trace by hand, which makes it the clearest place to see what a sorting algorithm promises: after pass , the largest values are in their final places. Its cost is the warning: 10 items need at most 45 comparisons, but 10,000 items need up to about 50 million. The early-exit check matters too, because it turns an already sorted list into a one-pass job.
Frequently asked questions
What is the Bubble Sort formula?
A simple sorting algorithm that repeatedly walks through the list, compares each pair of adjacent elements, and swaps them if they are in the wrong order. This process repeats until no more swaps are needed, meaning the list is fully sorted.
How do you use the Bubble Sort formula?
Bubble sort fits when the list is small (a few dozen items), when you are tracing a sort by hand, or when the list is nearly sorted with items only slightly out of place, since the early-exit check then finishes in a pass or two. Count its passes, comparisons and swaps when a question asks how a simple sort behaves. For large lists, use an method such as merge sort instead.
Why is the Bubble Sort formula important in CS Thinking?
Bubble sort is easy to trace by hand, which makes it the clearest place to see what a sorting algorithm promises: after pass , the largest values are in their final places. Its cost is the warning: 10 items need at most 45 comparisons, but 10,000 items need up to about 50 million. The early-exit check matters too, because it turns an already sorted list into a one-pass job.
What do students get wrong about Bubble Sort?
Bubble sort is O(n²) — avoid it for large data; use merge sort or quick sort instead.
What should I learn before the Bubble Sort formula?
Before studying the Bubble Sort formula, you should understand: sorting, iteration, efficiency.