Bubble sort

Po wants to teach bubble sort to students and juniors, with visuals, so that learners leave able to trace it by hand, see why it is correct, and know when not to use it. The payoff is a reusable, confident explanation rather than personal mastery for its own sake.

created
26 Aug 2026
status
active
lessons
6
references
2

Success looks like

  • Explain bubble sort in under three minutes with one diagram and no notes
  • Trace every pass of a 5-element array on a whiteboard without mistakes
  • State the loop invariant ("after pass k, the last k slots hold their final values") and use it to argue correctness
  • Write the optimized (early-exit) version in TypeScript from memory and explain why the best case is O(n)
  • Answer the classic learner questions: why n-1 passes, why it is stable, why nobody uses it in production, how it compares to insertion sort
  • Run a short interactive demo (visualizer) in front of a class

Constraints

Out of scope

Journey

  1. 1One pass, by handPredict every swap in a single pass10 min
  2. 2The invariantState the invariant and use it to argue correctness and n - 1 passes10 min
  3. 3Full algorithm and early exitWrite optimized TypeScript from memory; explain O(n) best case12 min
  4. 4Counting the costDerive n(n-1)/2 and show O(n²) growth visually10 min
  5. 5Stability and insertion sortExplain stability and compare with insertion sort12 min
  6. 6Teach itDeliver a 3-minute explanation with one diagram and run the demo20 min