Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Bubble Sort
function bubbleSort (sortingArray) {
var goto = sortingArray.length - 1;
for(var key in sortingArray) {
for(var i = 0; i <= goto - 1; i++) {
if(sortingArray[i] > sortingArray[i + 1]) {
// Swap
var temp = sortingArray[i];
sortingArray[i] = sortingArray[i +1];
sortingArray[i + 1] = temp;
}
}
goto--;
}
return sortingArray;
}
var unsorted = [456, 345, 234, 543, 465, 654];
console.log('Unsorted:', unsorted);
console.log('Bubble Sorted:', bubbleSort(unsorted));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment