Bubble Sort
Easy+50 XPSort by repeatedly swapping adjacent out-of-order pairs until settled.
No visualization loaded.
Watch
—
Press Run to begin.
Bubble sort is the most literal way to sort: walk along the row, and whenever two neighbours are in the wrong order, swap them. Do that pass after pass, and the biggest value 'bubbles' to the end each time, like a bubble rising in water. When a full pass makes zero swaps, everything's in order and you're done.
▸What does it mean to 'swap' two elements?
Swap = trade places. If box 2 holds 8 and box 3 holds 5, and they're out of order, you put 5 in box 2 and 8 in box 3. The values switch positions; nothing else changes. Sorting is really just a long series of these little swaps until everything's in order.
▸What's a 'pass', and why do we need several?
A pass is one full walk from left to right, swapping out-of-order neighbours as you go. One pass only guarantees the single biggest value reaches the end. The next-biggest needs another pass, and so on — so it can take many passes to fully sort.
▸Why is bubble sort O(n^2), and is that bad?
Roughly n passes, each looking at about n pairs, so the work is n×n = O(n²). For 1000 items that's about a million operations. It's fine for tiny or nearly-sorted data, but on big arrays O(n²) is slow — which is exactly why O(n log n) sorts like merge sort exist. Bubble sort is here to teach the idea, not to be fast.
▸Does it use extra memory?
No — it sorts 'in place,' rearranging the same array with swaps and no second copy. That makes it memory-cheap (its one redeeming quality). The cost is all in time, not space.
How the work grows
O(n²) is slow — explodes on big inputs. About n passes, each scanning about n pairs — so the work is n×n. Watch the curve shoot up: double the data and you roughly quadruple the work. It does sort in place, using no extra memory.
Faint dotted lines = O(1) (flat) and O(n) (straight) for comparison.