Quick Answer
Bubble sort puts a list in order by swapping neighbors. It compares 2 items at a time. Big values move to the end after every pass. This post shows 3 approaches. They are basic passes, early stop and C++. Worst case time is O(n squared). Sorted lists finish in O(n). Python and C++ code below was executed.
Bubble sort is the first sorting algorithm most learners meet. It is visual and you can do it by hand. That is why it is taught.
Real code has better choices and tests below show the gap. Sections below trace one list by hand and give 3 approaches with tested code.
What Is Bubble Sort?
It looks at 2 neighbors at a time. If the left one is bigger you swap them. Then you slide over by 1 and check the next pair.
Going down the entire list once is a pass. After the first pass the largest value sits at the end like a bubble that rose to the top. Later passes repeat the work on the part that is not yet sorted.
See the sorting algorithms lesson for a reference and the DSA roadmap for the learning order.
How Does Bubble Sort Work Step by Step?
Sort the list [5, 1, 4, 2]. Pass 1 compares 5 and 1 and swaps to give [1, 5, 4, 2]. Then 5 and 4 swap to give [1, 4, 5, 2] and 5 and 2 swap to give [1, 4, 2, 5].
Pass 2 compares 1 and 4 with no swap. Then 4 and 2 swap to give [1, 2, 4, 5]. Value 4 is now in place.
Pass 3 compares 1 and 2 and finds no swap. Passes with no swap mean the list is sorted.
Every pass fixes 1 more value at the end so passes get shorter.
How Do You Code Bubble Sort in Python and C++?
Code is 2 loops. Outer loop counts passes and inner loop walks the pairs and swaps them.
Approach #1: Basic Passes
What you get. Plain version. It always makes every pass even when the list is already sorted.
def bubble_sort(items):
n = len(items)
for i in range(n):
for j in range(n - i - 1):
if items[j] > items[j + 1]:
items[j], items[j + 1] = items[j + 1], items[j]
return items
print(bubble_sort([5, 1, 4, 2]))
Output is [1, 2, 4, 5].
Approach #2: Early Stop
What you get. Flag that stops the sort after a pass with no swap. Sorted lists finish in 1 pass.
def bubble_sort(items):
n = len(items)
for i in range(n):
swapped = False
for j in range(n - i - 1):
if items[j] > items[j + 1]:
items[j], items[j + 1] = items[j + 1], items[j]
swapped = True
if not swapped:
break
return items
print(bubble_sort([5, 1, 4, 2]))
print(bubble_sort([1, 2, 3, 4]))
Output is [1, 2, 4, 5] and then [1, 2, 3, 4]. Printing the list after every pass shows the trace from above.
items = [5, 1, 4, 2]
n = len(items)
for i in range(n):
swapped = False
for j in range(n - i - 1):
if items[j] > items[j + 1]:
items[j], items[j + 1] = items[j + 1], items[j]
swapped = True
print("pass", i + 1, items)
if not swapped:
break
It prints pass 1 [1, 4, 2, 5] then pass 2 [1, 2, 4, 5] then pass 3 [1, 2, 4, 5]. Pass 3 made no swap so the loop stopped there.
Approach #3: C++ Version
What you get. Same logic in C++ with a vector and a main function.
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
void bubbleSort(vector<int>& items) {
int n = items.size();
for (int i = 0; i < n; i++) {
bool swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (items[j] > items[j + 1]) {
swap(items[j], items[j + 1]);
swapped = true;
}
}
if (!swapped) break;
}
}
int main() {
vector<int> items = {5, 1, 4, 2};
bubbleSort(items);
for (int value : items) cout << value << " ";
cout << endl;
}
Output is 1 2 4 5.
Tested. Python 3.13.18 matched sorted() on 3,000 random lists for both Python versions. g++ 13.3 compiled the C++ version with warnings on. Tests ran in a cloud sandbox.
What Is the Time Complexity of Bubble Sort?
Worst case is a reversed list. It needs about n passes over n items so time is O(n squared).
Ideal case is a sorted list where the flag finishes in 1 pass so time is O(n). Space is O(1) because the sort works in place.
Test below counted comparisons and swaps on lists of 1,000 items.
| List of 1,000 items | Passes | Comparisons | Swaps |
|---|---|---|---|
| Reversed | 1,000 | 499,500 | 499,500 |
| Random | 956 | 498,554 | 247,244 |
| Sorted with early stop | 1 | 999 | 0 |
| Sorted without early stop | 1,000 | 499,500 | 0 |
Reversed 1,000 items need 499,500 comparisons. That is n times n minus 1 divided by 2 and not a full million. Flags cut a sorted list from 499,500 comparisons to 999.
When Should You Use Bubble Sort?
Use it to learn how comparing and swapping work. Swaps are easy to see and trace. For real data use the built in sort.
Test below compared Approach #2 with Python's sorted() on random lists. Table lists the lowest of 3 trials for the Python loop and 5 for sorted() in CPython 3.13.18.
| Items | Approach #2 | sorted() |
|---|---|---|
| 1,000 | 0.0299 s | 0.000062 s |
| 2,000 | 0.1021 s | 0.000145 s |
| 4,000 | 0.4404 s | 0.000356 s |
Doubling the items made it about 4 times slower. That matches O(n squared). Faster ideas like quick sort avoid that growth.
Other basic sorts share the same order of growth. Table below compares 3 of them on one random list of 1,000 items.
| Sort | Comparisons | Swaps or moves |
|---|---|---|
| Bubble | 498,554 | 247,244 swaps |
| Selection sort | 499,500 | 996 swaps |
| Insertion sort | 248,236 | 247,244 moves |
Selection sort makes far fewer swaps. See selection sort and insertion sort for the full walkthroughs.
Practice the pattern in the sorting practice set and open the bubble sort visualiser to watch the swaps.
Frequently Asked Questions
How Slow Is the Sort?
Worst and average time is O(n squared). Ideal time is O(n) on a sorted list with the flag. In the test 4,000 random items took 0.4404 s.
Why Is It Called Bubble Sort?
Large values rise to the end of the list one pass at a time like bubbles rising to the top. Every pass moves 1 more value into place.
Is It the Slowest Sorting Algorithm?
No. Selection sort and insertion sort have the same O(n squared) growth. It made 247,244 swaps in the test and selection sort made 996.
Is Bubble Sort Stable?
Yes. It swaps only when the left item is strictly bigger so equal items retain their order. Tests on 500 lists with repeated keys confirmed it.
How Does It Differ From Selection Sort?
It swaps neighbors again and again. Selection sort finds the smallest item and places it once per pass. Both are O(n squared).
Does Real Code Use Bubble Sort?
Built in sorts do better. Python's sorted() took 0.000062 s for 1,000 items while the Python loop took 0.0299 s.
What Does the Early Stop Do?
It ends the sort after a pass with no swap. Sorted lists of 1,000 items needed 999 comparisons with the flag and 499,500 without it.
What Should You Remember About Bubble Sort?
It compares neighbors and swaps them until the list is in order. It is clear to learn and slow to use.
Key takeaways.
- Worst case time is O(n squared) and ideal case time is O(n).
- Space is O(1) and the sort is stable.
- Built in sorts beat it by a wide margin.
Check yourself. Sorting [3, 2, 1] how many swaps does the sort make? Answer is 3.