Last active
April 23, 2017 21:55
-
-
Save pawarvijay/3e0bd194e677b9f07e8e9ad20f7d1766 to your computer and use it in GitHub Desktop.
JS Bin// source https://jsbin.com/kequnazawe
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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