Skip to content

Instantly share code, notes, and snippets.

@fortunee
Created February 19, 2019 10:44
Show Gist options
  • Save fortunee/1c89f22cb78ba102a70266a609841b29 to your computer and use it in GitHub Desktop.
Save fortunee/1c89f22cb78ba102a70266a609841b29 to your computer and use it in GitHub Desktop.
Selection sort algorithm
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