Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
function selectionSort(array) {
for (var i = 0; i < array.length; i++) {
let min = i; // storing the index of minimum element
for (var j = i + 1; j < array.length; j++) {
if (array[min] > array[j]) {
min = j; // updating the index of minimum element
}
}
if (i !== min) {
let temp = array[i];
array[i] = array[min];
array[min] = temp;
}
}
return array
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment