Skip to content

Instantly share code, notes, and snippets.

@zinevych
Created May 31, 2019 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zinevych/c7c0dc95c3a77d660ed9386bd71f23a9 to your computer and use it in GitHub Desktop.
Save zinevych/c7c0dc95c3a77d660ed9386bd71f23a9 to your computer and use it in GitHub Desktop.
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