Skip to content

Instantly share code, notes, and snippets.

@alexcambose
Last active January 12, 2019 09:39
Show Gist options
  • Save alexcambose/fefa286515e4c625d8a9e1640bb82f56 to your computer and use it in GitHub Desktop.
Save alexcambose/fefa286515e4c625d8a9e1640bb82f56 to your computer and use it in GitHub Desktop.
JavaScript bubble sort
let array = [3,1,4,5,3,2,6];
for(let i = 0; i < array.length; i++) {
for(j = i + 1; j < array.length; j++) {
if(array[i] > array[j]) {
// swap
const temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
console.log(array); // [1, 2, 3, 3, 4, 5, 6]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment