Skip to content

Instantly share code, notes, and snippets.

@LeeeeeeM
Created March 10, 2018 03:55
Show Gist options
  • Save LeeeeeeM/0cef9fd074f5d6429c749f90ff08ec9a to your computer and use it in GitHub Desktop.
Save LeeeeeeM/0cef9fd074f5d6429c749f90ff08ec9a to your computer and use it in GitHub Desktop.
选择排序
function selectionSort(array) {
var length = array.length;
for (var i = 0; i < length - 1; i++) {
var minIndex = i;
for(var j = i; j < array.length; j++) {
if (array[minIndex] > array[j]) {
minIndex = j;
}
}
if (minIndex !== i) {
swap(array, i, minIndex);
}
}
return array;
}
function swap(array, i, j) {
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment