- Explain why each pass places one value in its final position.
- Trace comparisons and swaps without running the code.
- Implement the optimized algorithm in JavaScript, Python, Java, or C++.
- Recognize when Bubble Sort is stable, useful, or too slow.
Sorting algorithms, made visible
See every swap.
Understand every pass.
Bubble Sort repeatedly compares neighbors and moves the larger value right — like a bubble rising to the surface.
Key idea After one full pass, the largest unsorted value reaches its final position.
Before you start
What you will learn
A clear finish line for the lesson, plus the small amount of knowledge you need before beginning.
- Arrays and zero-based indexes
- Loops and simple conditions
- Big O basics from the glossary
01 — The idea
How Bubble Sort thinks
It only needs one question: “Are these two neighbors in the right order?”
Compare neighbors
Start on the left and inspect two adjacent values.
Swap when needed
If the left value is larger, exchange their positions.
Repeat each pass
Continue until a whole pass finishes with no swaps.
02 — Try it yourself
Watch the algorithm work
Use your own numbers, then play the sort or move through one decision at a time.
Turn each animation into a written decision
| Step | Pass | Action | Pair | Array after step | Swaps |
|---|---|---|---|---|---|
| 0 | — | Ready | — | [72, 34, 91, 18, 56, 43, 27] | 0 |
The highlighted row always matches the visualization above. Step forward to extend the trace.
03 — Read the code
From idea to working code
Switch among JavaScript, Python, Java, and C++ to compare the same early-exit optimization.
01function bubbleSort(numbers) {02 const array = [...numbers];03 04 for (let pass = 0; pass < array.length - 1; pass++) {05 let swapped = false;06 07 for (let i = 0; i < array.length - pass - 1; i++) {08 if (array[i] > array[i + 1]) {09 [array[i], array[i + 1]] = [array[i + 1], array[i]];10 swapped = true;11 }12 }13 14 if (!swapped) break;15 }16 17 return array;18}04 — Avoid the traps
Common mistakes
These bugs often produce an answer that looks almost right. Learn the symptom and the correction together.
Comparing beyond the unsorted region
Symptom: Every pass scans the entire array again.
Correction: Stop before the values already locked at the right edge.
Using ≥ instead of >
Symptom: Equal values can cross and stability is lost.
Correction: Swap only when the left value is strictly greater.
Forgetting the early exit
Symptom: An already sorted array still takes quadratic time.
Correction: Track whether a pass swapped anything and stop when it did not.
Returning the wrong array
Symptom: The function unexpectedly mutates the caller's input.
Correction: Copy the input first when the function promises a new sorted array.
05 — Check yourself
Check your understanding
Answer 4 questions, then reveal the reasoning. Your progress stays on this device—no account required.
Answer every question to check your work.
06 — Mini game
Be the algorithm
Sort the packages from smallest to largest. Just like Bubble Sort, you may only swap adjacent neighbors.
Choose a package to begin.
07 — Use it wisely
Where Bubble Sort fits
Learning
Its neighbor-by-neighbor logic makes sorting fundamentals easy to see and debug.
Great fitTiny datasets
For a handful of values, clarity can matter more than performance.
Reasonable fitNearly sorted data
With the early-exit flag, one clean pass can finish in linear time.
Good special caseLarge datasets
Quadratic growth becomes expensive quickly. Prefer Merge Sort or Quick Sort.
AvoidLesson progress
Finish this lesson with confidence
Use this private checklist to confirm what you learned. Progress stays only in this browser.
Complete every checklist item to finish the lesson.