Skip to content

Instantly share code, notes, and snippets.

@jharris-code
Created January 30, 2019 18:04
Show Gist options
  • Save jharris-code/6201b8fbdc3e17e47ccebed591bb5237 to your computer and use it in GitHub Desktop.
Save jharris-code/6201b8fbdc3e17e47ccebed591bb5237 to your computer and use it in GitHub Desktop.
function bubbleSort(arr) {
let swapped = true;
while(swapped){
swapped = false;
for(let j = 0; j < arr.length - 1; j++){
if(arr[j] > arr[j+1]){
swapped = true;
let temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
return arr;
}
//outputs [1,2,3,4,5,6,7,8]
console.log(bubbleSort([1,2,5,3,7,8,6,4]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment