Bubble Sort — Algorithm Visualized
Learn bubble sort with step-by-step visuals — adjacent swaps, pass-by-pass animation, O(n²) complexity, code breakdown, and interactive bar chart.
Introduction
Bubble sort repeatedly compares adjacent elements and swaps them if out of order. After each pass, the largest unsorted value "bubbles" to the end — simple to understand, slow for large inputs at O(n²).
This guide covers:
- How bubble sort works — passes and adjacent swaps
- Step visualizer — bar chart per pass
- Full implementation — nested loops
- Complexity — best, average, worst
- When to use it — teaching and tiny datasets only
Quick index
| # | Section |
|---|---|
| 1 | How it works |
| 2 | Interactive visualizer |
| 3 | Full implementation |
| 4 | Complexity analysis |
| 5 | Real-life examples |
1. How it works
Pass 1: compare pairs → largest moves to end
Pass 2: ignore last element, repeat
...
Until no swaps needed| Phase | Action |
|---|---|
| Compare | Check arr[j] vs arr[j+1] |
| Swap | Exchange if left > right |
| Settle | After pass i, last i elements sorted |
2. Interactive visualizer
Bubble Sort — Step Visualizer
Adjacent comparisons and swaps — largest values bubble to the end each pass.
Unsorted
n = 7 elements
Step 1 — Input array
for (let i = 0; i < n - 1; i++)
for (let j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j+1]) swapRepeatedly swap adjacent out-of-order pairs.
Time
O(n²)
Space
O(1)
Stable
Yes
3. Full implementation
function bubbleSort(arr) {
const n = arr.length
for (let i = 0; i < n - 1; i++) {
let swapped = false
for (let j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
;[arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]
swapped = true
}
}
if (!swapped) break // already sorted
}
return arr
}Function calls with output
// ── Demo calls — output shown on the right ──
const arr = [5, 3, 8, 1, 9, 2, 7]
bubbleSort(arr) // → [1, 2, 3, 5, 7, 8, 9]
bubbleSort([3, 1, 2]) // → [1, 2, 3] (best case, early exit)
bubbleSort([5, 4, 3, 2, 1]) // → [1, 2, 3, 4, 5] (worst case)| Line block | Complexity |
|---|---|
| Outer loop | n − 1 passes |
| Inner loop | Up to n − i − 1 comparisons |
| Total | O(n²) worst/average |
4. Complexity analysis
| Case | Time | Notes |
|---|---|---|
| Best | O(n) | Already sorted + early exit |
| Average | O(n²) | Random input |
| Worst | O(n²) | Reverse sorted |
| Space | O(1) | In-place |
| Stable | Yes | Equal elements keep order |
5. Real-life examples
| Scenario | Why bubble sort fits |
|---|---|
| CS classrooms & whiteboards | Easiest to trace on paper — adjacent swaps are visible |
| Sorting 5–10 playing cards | n is tiny; simplicity beats O(n log n) overhead |
| Embedded sensors (n < 20) | Arduino reads 12 temperature samples — O(n²) is instant |
| Detecting if data is already sorted | Early-exit bubble sort returns in O(n) one pass |
| Introductory coding apps | Visualizers (like this one) teach comparison logic |
IoT temperature readings
Arduino collects 8 hourly readings — n is tiny so O(n²) bubble sort is instant.
Index → value
8 hourly readings from sensor — unsorted.
Real-world implementation
| function bubbleSort(arr) { | |
| const a = [...arr] | |
| for (let i = 0; i < a.length; i++) { | |
| let swapped = false | |
| for (let j = 0; j < a.length - 1 - i; j++) { | |
| if (a[j] > a[j + 1]) { | |
| [a[j], a[j + 1]] = [a[j + 1], a[j]] | |
| swapped = true | |
| } | |
| } | |
| if (!swapped) break | already sorted — O(n) exit |
| } | |
| return a | |
| } |
Full working code for the scenario above.
Function calls with output
// ── Function calls with output ── const readings = [22.1, 21.8, 23.0, 22.5, 21.9, 22.3, 22.0, 22.7]
Side output shows return value after each call — step through to trace execution.
Summary
Bubble sort teaches comparison-based sorting through adjacent swaps. It is stable and in-place but O(n²) — use it to learn, not to ship.
Next reads: Selection Sort · Insertion Sort · Big O Notation
Subscribe to my newsletter
Stay up to date and get notified when I share new contents.
No spam ever, unsubscribe anytime