Bubble Sort
Repeatedly compares neighbors and swaps them — the largest values “bubble” to the end.
Bubble Sort is like moving heavy boxes to the right: each pass pushes the biggest item to the end. It’s not fast for big data, but it’s one of the best algorithms for teaching comparison + swap logic because the motion is easy to see.
What It Does
Bubble Sort compares adjacent elements and swaps them if they’re in the wrong order. After each full pass, the largest remaining element ends up at the far right (its final position).
How It Works
- Start at the beginning of the list.
- Compare neighbors (A[j] and A[j+1]).
- Swap if they are out of order.
- Continue until the end of the pass — the largest value “bubbles” to the end.
- Repeat passes, each time stopping one position earlier (since the end is already sorted).
Why It’s Useful (and When It Isn’t)
Bubble Sort is mostly used for teaching because it clearly shows local comparisons and swaps. In real applications, it’s usually too slow for large datasets — but it’s great for building sorting intuition.
Performance Notes
- Time: worst/average
O(n²). - Best case:
O(n)with an “early-exit” flag (if no swaps happen in a pass). - Space:
O(1)extra space (in-place). - Stable: typically yes (equal values don’t have to swap).
Pitfalls (Student Mistakes)
- Wrong loop bounds: forgetting the inner loop should stop earlier each pass.
- Missing early-exit: not stopping when a pass makes no swaps (wastes time on nearly-sorted lists).
- Swapping logic: swapping the wrong pair or overwriting values (common when not using a temp variable).
- Assuming it’s “good enough”: it becomes painfully slow as n grows.
Demo / Media
Animation
The animation highlights neighbor comparisons and shows how the largest value moves right each pass. If your video includes early-exit, it will also show a pass where no swaps happen and the algorithm stops.
UX/UI • Pedagogy • HCI
A short teaching-focused rationale: what I designed, how I structured learning, and the interaction principles behind it.
- Information architecture: definition → steps → notes → pitfalls → demo.
- Visual hierarchy: separate “compare” and “swap” into clear blocks to prevent confusion.
- Clear CTA: “Watch Demo” scrolls to the video (low navigation cost).
- Consistency: same layout pattern across all algorithm pages.
- Accessibility: readable type/spacing + native video controls +
playsinline.
- Scaffolded learning: neighbors → comparison rule → swap → repeated passes.
- Three-act structure: what it is → how it works → why it matters (+ demo).
- Cognitive load: the “bubble to the end” story gives a simple anchor for beginners.
- Worked example: demo makes the repeated pass pattern visible.
- Self-check: pitfalls address loop bounds and early-exit misunderstanding.
- Mental models: “largest moves right” reinforces progress after every pass.
- Recognition over recall: repeating the same compare/swap pattern builds recognition quickly.
- Error prevention: pitfalls warn about common off-by-one and early-exit mistakes.
- Feedback cues: each swap visually confirms the comparison rule.
- Usability heuristics: clarity, consistency, predictable navigation.
Teaching strategy: scaffold → demonstrate → self-check. This supports beginner confidence while keeping the interface simple and consistent.