Loading algorithms…
Loading visualizer…
Repeatedly swap adjacent elements that are out of order. The simplest comparison sort — O(n²) worst case, but with an early-termination optimization that makes it linear on already-sorted input.
Adjacent swaps bubble the largest unsorted element toward the right
Bubble sort is the simplest comparison-based sorting algorithm. It repeatedly walks through the array, comparing each pair of adjacent elements and swapping them if they're out of order. After each full pass, the largest unsorted element has "bubbled" to the end of the array — hence the name.
(arr[j], arr[j+1]).arr[j] > arr[j+1]. Otherwise leave the pair alone.n − 1 passes have run.Bubble sort touches every pair on every pass. With n elements and n − 1 passes, that's O(n²) comparisons in the worst case. For arrays of size 1000, that's a million comparisons — versus ~10,000 for merge sort or quicksort. Bubble sort is mainly useful for teaching, not for production.
💡 Stable: Bubble sort uses an < comparison (strict greater-than for the swap), so equal elements never swap past each other. It's one of the few simple sorts that's natively stable.
Toggle 'Early termination' on to see how a single sorted pass can short-circuit the algorithm at the cost of an extra flag variable.