Last active
January 19, 2016 08:54
-
-
Save ramsunvtech/416a99bd7cf2836e5ff9 to your computer and use it in GitHub Desktop.
Bubble Sort in Javascript
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 len = arr.length; | |
for (var i = len-1; i>=0; i--){ | |
for(var j = 1; j<=i; j++){ | |
if(arr[j-1]>arr[j]){ | |
var temp = arr[j-1]; | |
arr[j-1] = arr[j]; | |
arr[j] = temp; | |
} | |
} | |
} | |
return arr; | |
} | |
bubbleSort([7,5,2,4,3,9]); //[2, 3, 4, 5, 7, 9] | |
bubbleSort([9,7,5,4,3,1]); //[1, 3, 4, 5, 7, 9] | |
bubbleSort([1,2,3,4,5,6]); //[1, 2, 3, 4, 5, 6] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment