Bubble Sort Visualizer — three modes + early exit

Bubble Sort Visualizer — with Explanation & Read Champak's implementation of Bubble Sort

Bubble Sort Visualizer — three modes + early exit

What does this visualizer show?

Bubble Sort repeatedly steps through the list, compares adjacent items and swaps them if they are in the wrong order. This visualizer supports left-to-right, right-to-left and alternating passes and performs an early exit when a complete pass makes no swaps. While sorting it announces comparisons, swaps and when elements become finalized (i.e. they have reached their sorted place). Time Complexity (Bubble Sort) - Best case: O(n) — when the array is already sorted and the algorithm detects no swaps in a full pass (early exit / adaptive). - Average case: O(n^2). - Worst case: O(n^2) — e.g., reversed array. Note: "n" is the number of elements. Space Complexity - Auxiliary space: O(1) — in-place algorithm (only a few variables for indices and a temporary swap variable). - No extra array is required for sorting. Two examples (step-by-step) Example 1 — 4 numbers (demonstrates swaps & finalization) Input: [4, 2, 1, 3] Pass 1 (left→right): - Compare 4 & 2 → swap → [2, 4, 1, 3] - Compare 4 & 1 → swap → [2, 1, 4, 3] - Compare 4 & 3 → swap → [2, 1, 3, 4] → 4 is finalized (largest) Pass 2: - Compare 2 & 1 → swap → [1, 2, 3, 4] - Compare 2 & 3 → no swap → [1, 2, 3, 4] → 3 finalized Pass 3: - Compare 1 & 2 → no swap No swaps across a full pass? If so, early exit (array sorted). Output: [1, 2, 3, 4] Example 2 — 2 numbers (small, quick) Input: [7, 2] Pass 1: - Compare 7 & 2 → swap → [2, 7] → 7 finalized No more pairs. Output: [2, 7] Why Bubble Sort matters (Importance) - Teaching & clarity: Bubble Sort is extremely simple and great for learning the ideas of comparisons,

(Highlights words while speaking)
What the dark-green border means:
A box with a dark-green border is finalized — it has reached its correct, sorted position and will not move again.
Ready...
The top (selected) entry is the newest event; the log records every compare and swap as the visual run proceeds.

0 Comments

Post a Comment

0 Comments

⬅ Go Home