Skip to content

Instantly share code, notes, and snippets.

@egoist
Created January 20, 2015 10:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save egoist/75e2da83e6a802908bf1 to your computer and use it in GitHub Desktop.
Save egoist/75e2da83e6a802908bf1 to your computer and use it in GitHub Desktop.
JavaScript 冒泡排序
// 冒泡排序
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