Algorithm Visualizer

Watch algorithms execute step by step. Select an algorithm, customize the input, and control the animation speed.

Bubble Sortsorting

Repeatedly steps through the list, compares adjacent elements and swaps them if they are in the wrong order.

Time: O(n²)Space: O(1)
Select Algorithms
640341252123224115906457

Step 1: Initial array. Starting Bubble Sort.

1 / 53Paused
Default
Comparing
Swapping
Sorted
Pivot
Found
Active
1function bubbleSort(arr) {
2 const n = arr.length;
3 for (let i = 0; i < n - 1; i++) {
4 for (let j = 0; j < n - i - 1; j++) {
5 if (arr[j] > arr[j + 1]) {
6 [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
7 }
8 }
9 }
10 return arr;
11}