Created
February 19, 2019 10:44
-
-
Save fortunee/1c89f22cb78ba102a70266a609841b29 to your computer and use it in GitHub Desktop.
Selection sort algorithm
This file contains hidden or 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
const selectionSort = function(array) { | |
let temp; | |
for(let i = 0; i < array.length; i++) { | |
let minIndexVal = i; | |
for(let j = i + 1; j<array.length; j++) { | |
if(array[j] < array[minIndexVal]) { | |
minIndexVal = j; | |
} | |
} | |
temp = array[i]; | |
array[i] = array[minIndexVal]; | |
array[minIndexVal] = temp; | |
} | |
return array; | |
}; | |
selectionSort([7, 4, 2, 1, 6, 3, 5]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment