Skip to content

Instantly share code, notes, and snippets.

@Ikhan
Created May 21, 2019 17:21
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 Ikhan/cf27e3074814f660d4cfb1482e33830e to your computer and use it in GitHub Desktop.
Save Ikhan/cf27e3074814f660d4cfb1482e33830e to your computer and use it in GitHub Desktop.
function selectionSort(arr) {
for (let i=0; i < arr.length; i++) {
let lowest = i;
for(let j = i+1; j < arr.length; j++) {
if (arr[j] < arr[lowest]) {
lowest = j;
}
}
if (i !== lowest) {
let temp = arr[i];
arr[i] = arr[lowest];
arr[lowest] = temp;
}
}
return arr;
}
selectionSort([3,10,11,8,1,2,6,9]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment