Interactive Visualizer
Sorting Algorithms Visualized
Select an algorithm, hit Play, and watch every comparison and swap come to life. Step through frames manually or let it run. Includes standard algorithms and the notorious Stalin Sort.
Standard
Quirky
Press Play to start sorting
SpeedMed
Default
Comparing
Swapping
Pivot
Sorted
Scanning
Heap
Bucket
Eliminated
Algorithm Details
Bubble Sort
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.
Complexity
Best Case
O(n)Average Case
O(n²)Worst Case
O(n²)Space
O(1)Code
"syn-keyword">function bubbleSort(arr) {
"syn-keyword">const n = arr.length;
"syn-keyword">for ("syn-keyword">let i = "syn-number">0; i < n - "syn-number">1; i++) {
"syn-keyword">for ("syn-keyword">let j = "syn-number">0; j < n - i - "syn-number">1; j++) {
"syn-keyword">if (arr[j] > arr[j + "syn-number">1]) {
"syn-comment">// Swap
[arr[j], arr[j + "syn-number">1]] = [arr[j + "syn-number">1], arr[j]];
}
}
}
"syn-keyword">return arr;
}