Skip to content

Instantly share code, notes, and snippets.

@sheniff
Created April 22, 2015 04:23
Show Gist options
  • Save sheniff/936b7507251eacda2b00 to your computer and use it in GitHub Desktop.
Save sheniff/936b7507251eacda2b00 to your computer and use it in GitHub Desktop.
BubbleSort in JS
function bubbleSort (arr) {
var l = arr.length,
tmp = null,
j = l - 1,
i;
if(l < 2) return arr;
for(; j >= 0; j--) {
for(i = 0; i < j; i++) {
if(arr[i] > arr[i+1]) {
// swap
tmp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = tmp;
}
}
}
return arr;
}
console.log(bubbleSort([9,3,1,6,65,32,2,1,1,4,9]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment