Skip to content

Instantly share code, notes, and snippets.

@qzm
Created October 9, 2017 02:04
Show Gist options
  • Save qzm/308d9261b6bc47e0d501cb5bbcbdcf1e to your computer and use it in GitHub Desktop.
Save qzm/308d9261b6bc47e0d501cb5bbcbdcf1e to your computer and use it in GitHub Desktop.
Bubble Sort
function bubbleSort(arr) {
// copy
var newArr = [];
for (var index = 0; index < arr.length; index++) {
newArr[index] = arr[index];
}
// sort
var temp;
for (var i = 0; i < newArr.length; i++) {
for (var j = 0; j < (newArr.length - i); j++) {
if (newArr[j] > newArr[j + 1]) {
temp = newArr[j];
newArr[j] = newArr[j + 1];
newArr[j + 1] = temp;
}
}
}
return newArr;
}
var sortList = bubbleSort([1, 1, 1, 1, 1, 0, 4, 78, 2, 789, 32, 7890, 32, 4]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment