Skip to content

Instantly share code, notes, and snippets.

@pawarvijay
Last active April 23, 2017 21:55
Show Gist options
  • Save pawarvijay/3e0bd194e677b9f07e8e9ad20f7d1766 to your computer and use it in GitHub Desktop.
Save pawarvijay/3e0bd194e677b9f07e8e9ad20f7d1766 to your computer and use it in GitHub Desktop.
function bubbleSort(items) {
console.log(items);
var length = items.length;
for (var i = (length - 1); i >= 0; i--) {
//Number of passes
for (var j = (length - i); j > 0; j--) {
console.log('items[j] = ' + items[j] + ' items[j - 1] = ' + items[j - 1])
if (items[j] < items[j - 1]) {
//Swap the numbers
var tmp = items[j];
items[j] = items[j - 1];
items[j - 1] = tmp;
console.log(items);
}
}
}
}
bubbleSort([ 9, 10 , 3 , 7 , 1, 4 , 8])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment