Skip to content

Instantly share code, notes, and snippets.

@minsooshin
Created July 6, 2016 05:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minsooshin/e31a1b0979d0e588b228d4b1459db5ef to your computer and use it in GitHub Desktop.
Save minsooshin/e31a1b0979d0e588b228d4b1459db5ef to your computer and use it in GitHub Desktop.
const swap = (array, a, b) => {
const temp = array[a];
array[a] = array[b];
array[b] = temp;
return array;
};
function bubbleSort(array) {
const n = array.length;
let swapped = false;
do {
swapped = false;
for (let i = 0; i < n; i++) {
if (array[i] > array[i + 1]) {
swap(array, i, i + 1);
swapped = true;
}
}
} while (swapped)
return array;
}
console.log(bubbleSort([76, 23, 4, 7, 11]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment