Problems178
Search for a command to run...
Adjacent comparison sort · largest element bubbles to end each pass
Try your own input
Change the array and replay the algorithm from step one.
Current step
Bubble Sort: repeatedly compare adjacent pairs and swap if out of order. Largest bubbles to the end each pass.
Bubble Sort
1function bubbleSort(arr) {//pass: 0swapped: —2 for (let i = 0; i < arr.length; i++) {3 let swapped = false;4 for (let j = 0; j < arr.length - i - 1; j++) {5 if (arr[j] > arr[j + 1]) {6 [arr[j], arr[j+1]] = [arr[j+1], arr[j]];7 swapped = true;8 }9 }10 if (!swapped) break;11 }12 return arr;13}