DAY 8 / 30 · WEEK 2 · PART 1 OF 3

Bubble Sort

Day 8 covers three O(n²) sorts in three short parts: Bubble Sort (here) → Selection SortInsertion Sort.

Goal of the day

Concept

Imagine sorting a hand of playing cards by repeatedly walking down the hand comparing each pair of neighbors, swapping any pair that's out of order. Every full walk pushes the largest remaining card as far right as it can go — like a bubble rising to the surface, which is where the name comes from. Walk the hand again, and the next-largest card sinks into place. Repeat until a full walk makes zero swaps — at that point, everything is sorted.

Diagram

Neighbors swap, largest sinks to the end each pass 5 2 8 1 9 ← 9 is already the biggest, sinks here first Each pass needs one fewer comparison than the last — the sorted tail never needs rechecking.

Interactive visualization

comparing swapping in final position
Press Play or Step to begin.

The code

function bubbleSort(arr) {
  for (let i = 0; i < arr.length - 1; i++) {
    let swapped = false;
    for (let j = 0; j < arr.length - 1 - i; j++) {
      if (arr[j] > arr[j + 1]) {
        [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
        swapped = true;
      }
    }
    if (!swapped) break; // a pass with zero swaps means the array is already sorted
  }
  return arr;
}

Line by line:

Dry run: bubbleSort([5, 2, 8, 1])

PassCompareSwap?Array after
15, 2Yes[2, 5, 8, 1]
15, 8No[2, 5, 8, 1]
18, 1Yes[2, 5, 1, 8]
22, 5No[2, 5, 1, 8]
25, 1Yes[2, 1, 5, 8]
32, 1Yes[1, 2, 5, 8]

Notice 8 reaches its final position after pass 1 and is never touched again — each pass needs to check one fewer element than the last.

Complexity

Time (best)Time (worst)SpaceStable?
O(n) — with the swapped-flag early exitO(n²)O(1)Yes

"Stable" means equal elements keep their original relative order. Bubble sort only ever swaps adjacent elements, so two equal values never leapfrog each other.

Interview corner

How to talk through it out loud: name the invariant bubble sort maintains — "after pass i, the last i elements are guaranteed to be in their final sorted position." Stating the invariant is what proves you understand why the algorithm terminates correctly, not just that you memorized the loop structure.

Practice problems

1. Squares of a Sorted Array (Easy — LeetCode: Squares of a Sorted Array)

Given a sorted array (possibly with negative numbers), return the squares of every element, also sorted.

2. Check if Array Is Sorted and Rotated (Easy — LeetCode: Check if Array Is Sorted and Rotated)

Given an array, return true if it could have been produced by taking a sorted array and rotating it some number of positions.

3. Sort Colors (Medium — LeetCode: Sort Colors)

Given an array with only the values 0, 1, and 2, sort it in place.

Quiz

1. Bubble sort's signature move each pass is:

2. What's bubble sort's WORST-CASE time complexity?

3. What's bubble sort's BEST-CASE time complexity, with the swapped-flag optimization?

4. Is bubble sort stable (do equal elements keep their original order)?

5. How commonly is bubble sort used in real production systems?

Continue to Part 2: Selection Sort →