← back to algorithms

dedicated visualizer

Bubble Sort

Repeatedly swaps adjacent out-of-order values so larger numbers drift to the end. This page keeps the runner, chart, and controls focused on a single algorithm so the walkthrough feels calmer than the overview page.

sortingbeginnerbest O(n)worst O(n²)space O(1)
adjacent comparisonswappingstablein-place

session controls

Compare this algorithm against a related one, turn on quiz mode, or keep the current state in a shareable URL.

current shareable URL

Copy the URL to preserve this exact dataset, target, compare mode, and quiz state.

browse more

Want a different problem or visual mode? Jump back to the catalog and open another dedicated page.

open catalog

scenario presets

Load a focused input that reveals a specific behavior quickly instead of hand-editing every value first.

dataset controls

Use your own array, randomize a fresh one, or restore defaults. The same dataset is shared by both panels in compare mode.

Enter up to 12 integers. Values are normalized to the range 1–99 for clean visualization.

step 1 / 490% complete

current action

start scan

8
8
idx 0
3
3
idx 1
5
5
idx 2
1
1
idx 3
9
9
idx 4
6
6
idx 5
2
2
idx 6
7
7
idx 7

run summary

Finished in 49 steps. Final order: 1, 2, 3, 5, 6, 7, 8, 9.

comparisons

27

swaps

14

passes

6

sorted

8

steps

49

current explanation

Bubble sort begins by scanning adjacent pairs from left to right.

simple explanation

Look at two neighboring numbers at a time.

pseudocode

1repeat passes from left to right
2compare neighboring values
3swap the pair if it is out of order
4stop if a full pass makes no swaps

complexity card

best

O(n)

average

O(n²)

worst

O(n²)

space

O(1)

algorithm notes

intuition

Bubble sort repeatedly fixes local inversions until the biggest values float to the right edge.

tradeoffs

  • Very easy to understand and animate.
  • Too slow for large datasets.
  • Early-exit optimization helps nearly sorted arrays.

when to use it

Use it mainly for learning and for discussing loop invariants, not for production sorting.

interview tips

  • Mention that bubble sort is stable and in-place.
  • Explain why the best case becomes linear with an early-exit flag.

what I learned building this

typed definitions

One algorithm schema now drives the catalog, counters, pseudocode, notes, and visual modes, which keeps the UI consistent as the lab grows.

replay over mutation

Precomputed steps made it much easier to synchronize explanations, metrics, quiz prompts, and scrubber playback without hidden state drifting out of sync.

portfolio framing

Shareable URL state, compare mode, and responsive layouts mattered as much as the algorithm logic because this page needs to teach clearly and still feel polished as a product.

more in this lane

Want a different take on the same problem family? These stay in the same category but change the strategy.