Summary
Bubble sort puts a list in order by comparing two items at a time and swapping them if they are out of order. It repeats this pass after pass until nothing needs swapping. The biggest values bubble to the end first. It is easy to learn but slow, so it is taught far more than it is used.
Bubble sort is the first sorting algorithm almost everyone learns. It is simple, it is visual, and you can do it by hand. That is exactly why it is taught.
But here is the honest part most tutorials leave out. Almost nobody uses bubble sort in real code. So let me show you how it works, why it is slow, and the one trick that makes it less bad.
What is bubble sort?
Bubble sort is a way to put a list in order by looking at two neighbours at a time. If the left one is bigger than the right one, you swap them. Then you slide over by one and check the next pair.
You do this all the way down the list. That is one pass. After the first pass, the largest value has moved to the end, like a bubble rising to the top. Then you do it again for the rest, and again, until the whole list is sorted.
The name comes from that bubbling. Big values float to the end one pass at a time. It is one of the first algorithms in any DSA roadmap, right next to selection sort.
How does bubble sort work step by step?
Reading the rule is not enough. You have to watch the swaps. So let’s sort the list [5, 1, 4, 2] by hand, one pass at a time.
Start the first pass. Compare 5 and 1. Since 5 is bigger, swap them to get [1, 5, 4, 2]. Slide over. Compare 5 and 4, swap to get [1, 4, 5, 2]. Slide over. Compare 5 and 2, swap to get [1, 4, 2, 5]. The pass is done, and 5 has bubbled to the end.
Now the second pass, over the first three items. Compare 1 and 4, no swap. Compare 4 and 2, swap to get [1, 2, 4, 5]. The second largest value, 4, is now in place. One more short pass confirms 1 and 2 are in order, and the list is sorted.
Notice the pattern. Each pass locks one more value at the end, so each pass can be a little shorter than the last. That is the whole method.
Bubble sort code in Python and C++
The code is two loops. The outer loop counts the passes. The inner loop walks the pairs and swaps them. Here it is in Python, with the important trick included.
def bubble_sort(arr):
n = len(arr)
for i in range(n):
swapped = False
for j in range(n - i - 1):
if arr[j] > arr[j + 1]:
arr[j], arr[j + 1] = arr[j + 1], arr[j]
swapped = True
if not swapped: # nothing swapped, list is sorted
break
return arr
That swapped flag is the trick most tutorials skip. If a full pass makes no swaps, the list is already sorted, so you stop early. Here is the same code in C++, the language most Indian placement tests expect.
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n; i++) {
bool swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr[j], arr[j + 1]);
swapped = true;
}
}
if (!swapped) break; // already sorted, stop early
}
}
Both versions shrink the inner loop with each pass, since the end of the list is already sorted. The early stop keeps an almost sorted list from wasting time.
What is the time complexity of bubble sort?
This is where bubble sort earns its bad name. In the worst case, with the list in reverse order, it makes about n passes over n items, which is O(n squared) time. For a list of 1,000 items, that is close to a million comparisons.
The best case is much nicer. If the list is already sorted, the swapped flag lets it finish in a single pass, which is O(n). The space is O(1), since it sorts in place with no extra memory. You can read the formal details on Wikipedia’s bubble sort page.
The honest truth about bubble sort
Real programs almost never use bubble sort. That O(n squared) cost makes it far too slow for big lists, and faster sorts win every time. So why learn it? Because it is the clearest way to see how sorting and swapping work. Treat it as a teaching tool, not a tool for real work. When speed matters, you reach for something better.
When should you use bubble sort?
Be honest about this one. For real work, almost never. It is too slow for large data, and built in sort functions are both faster and easier. If you are sorting real data, use those instead.
It does have two fair uses. It is excellent for learning, because the swaps are so easy to see and trace. And with the early stop trick, it can check whether a tiny, nearly sorted list needs any work at all. Beyond that, faster sorts like quick sort are the real answer, and even selection sort makes fewer swaps.
FAQ
What is the time complexity of bubble sort?
O(n squared) in the worst and average cases, because it makes many passes over the list. The best case is O(n) when the list is already sorted and the early stop kicks in.
Why is it called bubble sort?
Because the largest values rise to the end of the list one pass at a time, like bubbles floating to the top. Each pass bubbles up one more value.
Is bubble sort the slowest sorting algorithm?
It is among the slowest practical ones at O(n squared). Selection sort and insertion sort share that class, but bubble sort usually makes the most swaps, so it is often the slowest of the three in practice.
Is bubble sort stable?
Yes. It never swaps equal items, so two equal values keep their original order. That property is called stability, and bubble sort has it.
What is the difference between bubble sort and selection sort?
Bubble sort swaps neighbours repeatedly, making many swaps. Selection sort finds the smallest item and places it once per pass, making far fewer swaps. Both are O(n squared).
Do real programs use bubble sort?
Almost never. It is too slow for real data. Programs use faster built in sorts. Bubble sort lives on as a teaching tool because it is so easy to understand.
So what should you remember?
Bubble sort compares neighbours and swaps them until the list is in order, bubbling the big values to the end. It is the clearest sort to learn and one of the slowest to use.
Learn it to understand how sorting and swapping feel, then move on to faster methods for real work. The early stop trick is the one piece worth keeping in your head.
Now try it. Sorting the list [3, 2, 1], how many swaps does bubble sort make to finish?