Skip to content

Instantly share code, notes, and snippets.

@joelbarbosa
Created February 28, 2019 03:10
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 joelbarbosa/473d75d316a762b19eb3c0c6868d0856 to your computer and use it in GitHub Desktop.
Save joelbarbosa/473d75d316a762b19eb3c0c6868d0856 to your computer and use it in GitHub Desktop.
Bubble Sort
function bublesort(arr1) {
const arr = [...arr1];
let swap
do {
swap = false;
for (i=0; i < arr.length-1; i++) {
if (arr[i] > arr[i+1]) {
let tmp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = tmp;
swap = true
}
}
}while(swap);
return arr;
}
const a1 = [1, 2, 4, 0, 10, 3, 6, 5];
console.log(bublesort(a1))
// alternative sort
function _sort(a, b) {
return a - b;
}
const sort = a1.sort(_sort)
console.log(sort)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment