Skip to content

Instantly share code, notes, and snippets.

@isramv
Last active August 29, 2015 14:20
Show Gist options
  • Select an option

  • Save isramv/50359eb941ce28582e47 to your computer and use it in GitHub Desktop.

Select an option

Save isramv/50359eb941ce28582e47 to your computer and use it in GitHub Desktop.
JavaScript Bubble Sort Algorithm
arraynumbers = [5,2,1,4,9,12,4,3,2,12];
completed = false;
while(!completed) {
function reOrder() {
for (var i = 0; i < arraynumbers.length; i++) {
if(compareTwo(arraynumbers[i], arraynumbers[i+1]) || isEqual(arraynumbers[i], arraynumbers[i+1])) {
tmp = arraynumbers[i+1];
arraynumbers[i+1] = arraynumbers[i];
arraynumbers[i] = tmp;
console.log(arraynumbers);
reOrder();
} else {
completed = true;
}
}
}
reOrder(arraynumbers);
completed = true;
}
function compareTwo(a, b) {
if(a > b) {
return true;
} else {
return false;
}
}
function isEqual(a, b) {
if(a == b) {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment