Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created August 17, 2023 12:45
Show Gist options
  • Save codecademydev/7cb2dc126195f98def5e535ab88a8249 to your computer and use it in GitHub Desktop.
Save codecademydev/7cb2dc126195f98def5e535ab88a8249 to your computer and use it in GitHub Desktop.
Codecademy export
const swap = require("./swap");
const bubbleSort = (input) => {
let swapCount = 0;
let swapping = true;
while (swapping) {
swapping = false;
for (let i = 0; i < input.length - 1; i++) {
if (input[i] > input[i + 1]) {
swap(input, i, i + 1);
swapCount++;
swapping = true;
}
}
}
console.log(`Swapped ${swapCount} times`);
return input;
};
const arr1 = [9, 8, 7, 6, 5, 4, 3, 2, 1];
const arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9];
bubbleSort(arr1);
bubbleSort(arr2);
module.exports = bubbleSort;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment