Created
March 10, 2018 03:55
-
-
Save LeeeeeeM/0cef9fd074f5d6429c749f90ff08ec9a to your computer and use it in GitHub Desktop.
选择排序
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 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