Sorting · start from zero
Bubble Sort,
one step at a time.
The idea, one sweep traced by hand, the code, a demo you control step by step, and the interview questions that actually come up in placements. Nothing plays on its own — you press Next.
Run the sorting simulation →01 The idea
What bubble sort does
Sorting means putting numbers in order, smallest to largest. Bubble sort does it the simplest way you can imagine: it only ever looks at two numbers standing next to each other.
02 See one sweep
Watch the biggest number rise
One left-to-right sweep across 5 1 4 2. Follow the 5 — it keeps getting carried right until it lands at the end.
Three comparisons moved the 5 into its final spot. Notice two things, because the next section turns both into one line of code each: the last box is now settled and never needs checking again, and if a sweep had made no swaps at all, the list was already in order.
03 The code
Naive first, then two small fixes
The naive version keeps sweeping the whole list every time. The optimised version adds two tiny changes, and both came straight from what you just saw by hand.
Naive
for (i = 0; i < n-1; i++) for (j = 0; j < n-1; j++) if (a[j] > a[j+1]) swap(a[j], a[j+1]);
Optimised
for (i = 0; i < n-1; i++) { swapped = false; for (j = 0; j < n-1-i; j++) if (a[j] > a[j+1]) { swap(a[j], a[j+1]); swapped = true; } if (!swapped) break;}
Fix 1 — j < n-1-i: the trace showed the tail is already settled after each sweep, so the inner loop should stop earlier every time instead of re-checking boxes that are done.
Fix 2 — the swapped flag: if a whole sweep makes no swaps, nothing was out of order, so break. This is what makes an already-sorted list finish in a single pass.
05 Cheat sheet
The numbers to remember
| Case | Time | When |
|---|---|---|
| Best | O(n) | Already sorted — one sweep, no swaps, break |
| Average | O(n²) | Random order |
| Worst | O(n²) | Reversed list |
| Space | O(1) | In place — one temp variable |
06 Where & why
When to actually use bubble sort
Be honest in an interview: bubble sort is almost never the fastest choice. Its value is being simple, stable, and in-place, so it earns a place only when that trade-off is worth it. Knowing when not to use something is most of what an interviewer is checking.
Reach for it when…
Use something else when…
| Situation | Better choice | Why |
|---|---|---|
| Large dataset | Quicksort / Merge sort | O(n log n) instead of O(n²) — the gap explodes as n grows |
| Guaranteed worst case needed | Merge sort / Heap sort | Always O(n log n); quicksort can degrade to O(n²) |
| Large but nearly sorted | Insertion sort / Timsort | Fewer moves, still simple; Timsort is built for exactly this |
| Big data, tight memory | Heap sort | O(n log n) time with O(1) extra space |
| Real production code | The built-in sort | Timsort and introsort are tuned and battle-tested |
07 Interview questions
What placements actually ask
The conceptual questions that come up in coding rounds and vivas. Tap a question to see a crisp answer — each one is short enough to say out loud in about twenty seconds.
How does bubble sort work, in one line?
What is its time complexity — best, average, worst?
Why is the best case O(n) and not O(n²)?
What is the space complexity?
Is bubble sort stable?
Is it adaptive?
How many comparisons and swaps in the worst case?
Bubble vs selection vs insertion sort?
What is the key optimisation to mention?
When would you actually use it?
08 Practice problems
Same idea, with a twist
Each of these is solvable with the bubble sort idea, but bends it slightly — exactly the kind of variation interviewers use to check real understanding. Try them, then reveal the hint.