Step through one of the most intuitive sorting algorithms where larger elements "bubble up" to the end.
Parameters
12
Visualization
Step: 0
Click Randomize or Step to begin sorting.
Algorithm Insight
Bubble Sort repeatedly swaps adjacent elements if they are in the wrong order.
1.Compare two adjacent elements.
2.If $Left > Right$, swap them.
3.Move to the next pair.
4.After one pass, the largest element is at the end.
Complexity
Average CaseO(N²)
Space ComplexityO(1)
Deconstructing Bubble Sort
How this simple, intuitive sorting algorithm works. Use this guide after interacting with the visualizer.
Quick Context: What is Bubble Sort?
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. The algorithm gets its name because smaller or larger elements "bubble" to their proper place.
The Core Idea: Compare and Swap
The entire algorithm is built on one fundamental operation: comparing two adjacent items and swapping them if the first is larger than the second. This process is repeated from the beginning of the array to the end. After the first full pass, the largest element in the array will have "bubbled up" to the very last position. The next pass does the same for the second-largest element, and so on.
Guided Experiments with the Visualizer
One Full Pass: Click "Randomize Array", then click "Next Step" repeatedly. Watch the two active bars (the ones being compared). Notice how the larger value always moves to the right after a swap. After you've gone through the whole array once, observe that the largest bar is now at the far right, in its final sorted position.
The Sorted Section: As you complete each full pass, you'll see the sorted elements accumulate at the end of the array. The algorithm is smart enough to know it doesn't need to check this sorted section again, making each subsequent pass slightly shorter.
Best Case Scenario: Manually create a nearly sorted array (or hope "Randomize" gives you one!). Start the sort. An optimized Bubble Sort can detect if no swaps were made during a full pass. If so, it knows the array is already sorted and can stop early.
Worst Case Scenario: A reverse-sorted array is the worst case. Every single comparison will result in a swap. Run this to see the maximum number of steps the algorithm can take.
Pseudocode
// Basic Bubble Sort
function bubbleSort(array) {
n = array.length;
for (i from 0 to n-1) {
for (j from 0 to n-i-2) {
if (array[j] > array[j+1]) {
swap(array[j], array[j+1]);
}
}
}
}
The outer loop `i` tracks how many elements are already sorted at the end. The inner loop `j` performs the adjacent comparisons.
Performance & When to Use It
Bubble Sort has a time complexity of O(n²) in the average and worst cases, which is very slow for large datasets. Its main advantages are its simplicity and the fact that it requires O(1) extra space.
Because of its poor performance, Bubble Sort is rarely used in production systems. It serves primarily as an educational tool to introduce sorting concepts and the idea of algorithmic analysis.
Cheat sheet
Bubble Sort
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The pass through the list is repeated until the list is sorted. The algorithm gets its name because smaller or larger elements "bubble" to their proper place.