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.

Mini example[ 5, 2, 4 ]
01
524
compare
02
254
swap
03
245
sorted

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.

Learning objectives
  1. Explain why each pass places one value in its final position.
  2. Trace comparisons and swaps without running the code.
  3. Implement the optimized algorithm in JavaScript, Python, Java, or C++.
  4. Recognize when Bubble Sort is stable, useful, or too slow.
01Understand02Visualize03Trace04Code05Complexity06Quiz07Challenge08Use cases

01 — The idea

How Bubble Sort thinks

It only needs one question: “Are these two neighbors in the right order?”

1
7vs3

Compare neighbors

Start on the left and inspect two adjacent values.

2
3⇄7

Swap when needed

If the left value is larger, exchange their positions.

3
357

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.

3–10 values, each from 1 to 99
Pass — Paused
→

Ready. Start the first left-to-right pass.

Comparisons0
Swaps0
Progress0%
Comparing Sorted
Dry-run trace

Turn each animation into a written decision

The latest eight Bubble Sort decisions
StepPassActionPairArray after stepSwaps
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.

bubble-sort.mjs
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.

01

Comparing beyond the unsorted region

Symptom: Every pass scans the entire array again.

Correction: Stop before the values already locked at the right edge.

02

Using ≥ instead of >

Symptom: Equal values can cross and stability is lost.

Correction: Swap only when the left value is strictly greater.

03

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.

04

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.

0 of 4 answered
Q1What is guaranteed after one complete left-to-right pass?
Q2With the early-exit flag, what is the best-case time complexity?
Q3Which comparison preserves the order of equal values?
Q4Starting with [3, 1, 2], what follows the first comparison?

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.

Rule Select one package, then select a neighbor to swap them.
Sort the conveyor0 moves
↔

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 fit
≋

Tiny datasets

For a handful of values, clarity can matter more than performance.

Reasonable fit
↻

Nearly sorted data

With the early-exit flag, one clean pass can finish in linear time.

Good special case
×

Large datasets

Quadratic growth becomes expensive quickly. Prefer Merge Sort or Quick Sort.

Avoid

Lesson progress

Finish this lesson with confidence

Use this private checklist to confirm what you learned. Progress stays only in this browser.

0 of 4 complete0%

Complete every checklist item to finish the lesson.