DSA Visualizer
SORT

Sorting Visualizer

Watch a sort run one step at a time. Press play and follow the bars, the highlighted pseudocode, and the running tally of work done.

Repeatedly swaps adjacent out-of-order pairs. The simplest sort to picture — but O(n²) and slow.

8
3
12
5
9
1
6
11
2
7
Bubble sort compares each neighbouring pair and swaps them if they're out of order, so big values 'bubble' to the right.
0
Comparisons
0
Swaps
0
Writes
1 / 85

Pseudocode

Bubble Sort
1for i in 0 .. n-2:
2 swapped = false
3 for j in 0 .. n-2-i:
4 if a[j] > a[j+1]:
5 swap(a[j], a[j+1])
6 swapped = true
7 if not swapped: break // already sorted

Legend

ComparingSwappingSorted
Data

Tip: Space play/pause · step

Learn sorting

Sorting arranges items into order (here, smallest → largest). All three sorts on this page are comparison sorts: they decide what goes where purely by comparing pairs of values.

They're also in-place (they reuse the same array, O(1) extra memory) and simple to reason about. The trade-off is speed: each runs in O(n²) on average, so they shine on small or nearly-sorted inputs rather than huge datasets.

Stable

Keeps equal values in their original relative order. Bubble & insertion are stable; selection is not.

In-place

Sorts within the array itself — no second array needed, so memory stays at O(1).