Created
January 20, 2015 10:09
-
-
Save egoist/75e2da83e6a802908bf1 to your computer and use it in GitHub Desktop.
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(array) { | |
var temp; | |
for (var i = 0; i < array.length - 1; i++) { | |
for (var j = 0; j < array.length - i - 1; j++) { | |
if (array[j] > array[j+1]) { | |
temp = array[j]; | |
array[j] = array[j+1]; | |
array[j+1] = temp; | |
} | |
} | |
} | |
return array; | |
} | |
// test | |
var test = [1, 2, 22, 64, 4, 12, 56, 89, 7, 9, 85]; | |
console.log(bubbleSort(test)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment