Bubble sort is the simplest sorting algorithm to understand: it walks through the array comparing each pair of neighbours and swaps them if they are in the wrong order. After each pass the largest remaining value has “bubbled up” to its final place at the end. This visualizer animates every comparison and swap as moving bars, with the current pseudocode line highlighted.
Try a reversed array to see the worst case, where every comparison leads to a swap, and a nearly sorted array to see the early exit: if a whole pass makes no swaps, the array is already sorted and the algorithm stops. The counters show how quickly the work grows, about n²/2 comparisons for n values.
How to use the Bubble Sort Visualizer tool
- Pick an array size and an input: random, nearly sorted, reversed or few unique values. You can also type your own numbers.
- Press Play to watch the sort, or Step to move one comparison or swap at a time. Back goes one step back.
- Follow the colours: orange bars are being compared, red bars are being swapped or written, green bars are in their final place.
- Read the message under the controls and the highlighted pseudocode line to see exactly what the algorithm is doing.
- Change the speed, switch the code tab to C++, Java, Python or JavaScript, and compare the counters with other inputs.
Why use ToolOrca Bubble Sort Visualizer
- Animated bars with play, pause, step and step back
- Speed slider and a position slider to jump to any step
- Comparison and swap counters
- Random, nearly sorted, reversed and few-unique inputs, or your own numbers
- Highlighted pseudocode line for every step
- Code in C++, Java, Python and JavaScript
- Works on phones and tablets, keyboard accessible
How bubble sort works
Bubble sort makes repeated passes over the array. In each pass it compares a[j] and a[j + 1] for every j and swaps them when a[j] is larger. The biggest value keeps winning its comparisons, so it moves all the way to the end in the first pass. The second pass does the same for the second biggest value, and so on, so each pass can stop one position earlier.
The optimized version, shown here, remembers whether a pass swapped anything. If not, every neighbour pair is in order, so the whole array is sorted and the algorithm stops early. That is what makes bubble sort O(n) on an array that is already sorted.
Time and space complexity of bubble sort
In the worst case (a reversed array) and on average, bubble sort makes about n(n − 1)/2 comparisons and a similar number of swaps, which is O(n²): sorting 1,000 values takes around 500,000 comparisons. The best case, an already sorted array, needs one pass of n − 1 comparisons: O(n). It sorts in place with O(1) extra memory and is stable, because equal values are never swapped.
When to use bubble sort
Bubble sort is mainly a teaching tool. It is easy to code and to prove correct, but slower than insertion sort on almost every input. Use insertion sort for small or nearly sorted arrays, and merge sort, quick sort or your language’s built-in sort for real data. Compare them all in the sorting algorithm race.
Every visualizer on this site runs in your browser with the same controls: Play, Pause, Step forward, Step back and a speed slider. You can also drag the position slider to jump to any step, or focus the drawing and use the Space bar and arrow keys.
Frequently asked questions
What is the time complexity of bubble sort?
O(n²) in the average and worst case, and O(n) in the best case when the array is already sorted and the early-exit check is used.
Is bubble sort stable?
Yes. It only swaps neighbours when the left one is strictly greater, so equal values keep their original order.
Why is it called bubble sort?
Because large values rise to the end of the array like bubbles rising to the top of water, one swap at a time.
How many passes does bubble sort need?
At most n − 1 passes for n values. With the swapped flag it stops as soon as a pass makes no swaps.
Is bubble sort ever used in practice?
Rarely. Insertion sort does the same job faster. Bubble sort survives because it is a clear first example of a sorting algorithm and of O(n²) growth.