Created
April 22, 2015 04:23
-
-
Save sheniff/936b7507251eacda2b00 to your computer and use it in GitHub Desktop.
BubbleSort in JS
This file contains 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 (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