The first time I tried to sort a list of numbers in code, I wrote ten lines of confused if-statements and still got the wrong order. Then a friend showed me bubble sort, and I laughed β€” because the whole idea fits in one sentence: keep comparing neighbours, and let the biggest number float to the top like a bubble in water. It is the friendliest sorting algorithm you will ever meet, and once you see it work on a real example, you will never forget it. Let’s understand bubble sort step by step, with an example you can follow.

📅 Want a study plan for this topic?

Get a free 7-day learning plan in your calendar — one small lesson every day.

What Is Bubble Sort?

Bubble sort is a simple sorting algorithm β€” a recipe that puts a jumbled list into order, smallest to largest. It works by walking through the list again and again, comparing each pair of neighbours and swapping them whenever they are in the wrong order. After every full walk-through, called a pass, the biggest number has "bubbled up" to its final spot at the end. The algorithm repeats until one whole pass finishes with zero swaps, which means everything is finally in order.

The Main Idea

Picture a glass of fizzy drink. The biggest bubbles rise to the top first, then the next biggest, and so on. Bubble sort does the same thing with numbers: the largest value rises to the end of the list on the first pass, the second-largest on the next pass, and the list slowly settles into order. It is not the fastest sorting method β€” far from it β€” but honestly, it is the best one to learn first, because you can watch every single decision it makes.

In Simple Words

Here is the whole recipe, in four steps that repeat:

  1. Compare two neighbours, side by side.
  2. Swap them if the left one is bigger than the right one.
  3. Move on to the next pair, all the way to the end of the list.
  4. Repeat from the start until a full pass makes no swaps at all.

When I first tried this by hand, I kept comparing the wrong pairs and skipping numbers. Most beginners get stuck right here: you must compare every neighbouring pair on every pass, even the ones that already look fine. Skip one pair, and a number stays out of place. The good news? The rule never changes, so after one worked example it becomes automatic.

Let's Sort [5, 2, 9, 1] Together

Here is our messy little list: [5, 2, 9, 1]. Four numbers, three passes, and you will see exactly how bubble sort tames them. Watch each pair:

numbers = [5, 2, 9, 1]
PassPairs comparedWhat happenedList after the pass
15 vs 2 β†’ swap; 5 vs 9 β†’ keep; 9 vs 1 β†’ swap9 bubbles to the end[2, 5, 1, 9]
22 vs 5 β†’ keep; 5 vs 1 β†’ swap5 settles into place[2, 1, 5, 9]
32 vs 1 β†’ swapSorted β€” no more swaps needed[1, 2, 5, 9]

That is the whole show. Three passes, five comparisons, three swaps β€” and the list is sorted. In my opinion, the smartest detail is the stopping rule: if a pass finishes with zero swaps, the algorithm quits early instead of marching through pointless extra passes. Programmers call this the swapped flag, and adding it turns a naive bubble sort into a slightly clever one.

Diagram: bubble sort sorting 5, 2, 9, 1 step by step, pass by pass

Animated: bubble sort sorting 5, 2, 9, 1 one swap at a time

Bubble Sort in Python

Here is the whole algorithm in five lines of real Python:

numbers = [5, 2, 9, 1]
for i in range(len(numbers)):
    for j in range(len(numbers) - 1 - i):
        if numbers[j] > numbers[j + 1]:
            numbers[j], numbers[j + 1] = numbers[j + 1], numbers[j]
print(numbers)  # [1, 2, 5, 9]

The outer loop runs one pass at a time; the inner loop walks through the pairs. Notice the len(numbers) - 1 - i part β€” after each pass, the last i numbers are already sorted, so there is no point comparing them again. If you have followed my Python lists and loops guide, this code should look comfortingly familiar: it is just two loops and one if-statement doing all the work.

How Fast Is Bubble Sort, Really?

Here is where I have to be honest with you: bubble sort is slow. For a list of n items it makes roughly nΒ² comparisons in the worst case, which computer scientists write as O(nΒ²) β€” the same time complexity notation I explained in my Big-O guide. Sorting 10 items takes about 100 steps, but sorting 1,000 items takes about a million. That is why real software uses faster algorithms like quicksort for big jobs.

List sizeComparisons (roughly)Verdict
10 items~45Instant β€” bubble sort is fine
100 items~5,000Still quick, but wasteful
10,000 items~50 millionUse a faster algorithm

So why learn it at all? Because every faster algorithm builds on the same ideas β€” comparing, swapping, and knowing when to stop. Once you can trace bubble sort on paper, algorithms like binary search feel much less mysterious: they are all just clever ways of organising the same simple comparisons.

Test Your Understanding

Quiz: After the FIRST pass of bubble sort on [5, 2, 9, 1], what does the list look like?

  1. [1, 2, 5, 9]
  2. [2, 5, 1, 9]
  3. [5, 2, 1, 9]
  4. [2, 1, 5, 9]

Answer: B. The first pass swaps 5 and 2, keeps 5 and 9, then swaps 9 and 1 β€” so 9 bubbles to the end and the list becomes [2, 5, 1, 9].

Conclusion

Bubble sort will never win a speed contest, but it wins something better: it makes sorting visible. Compare neighbours, swap when they are wrong, repeat until nothing moves. Try it yourself β€” grab four numbers, sort them on paper, then run the Python code above and watch the same steps happen in a blink. The next time you meet a fancier algorithm, you will already know the moves.

📅 Want a study plan for this topic?

Get a free 7-day learning plan in your calendar — one small lesson every day.

Related Articles

Never miss a new article

Subscribe to The Geeks Guru newsletter and get every new article in your inbox.

Loading