Sorting TechniqueCore
Bubble Sort
JavaScript Solution & Complexity Analysis
Run & test in browserJavaScript Lab Sandbox
function swap(arr, a, b) {
let temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
};
function bubbleSort(arr) {
let n = arr.length;
for (let i = 0; i < n - 1; i++) {
let swapped = false;
for (let j = 0; j < n - 1 - i; j++) {
if (arr[j + 1] < arr[j]) {
swap(arr, j, j + 1);
swapped = true;
}
}
if (!swapped) break;
}
}
let arr = [4, 1, 3, 9, 7]
console.log("Before Bubble Sort", arr);
bubbleSort(arr);
console.log("After Bubble Sort", arr);Console Output
Click "Run Code" above to execute and view output...