Skip to content

Instantly share code, notes, and snippets.

@oliverbth05
Created August 29, 2018 13:32
Show Gist options
  • Save oliverbth05/73eaaf09e2495735f7b4c0c49e8a029f to your computer and use it in GitHub Desktop.
Save oliverbth05/73eaaf09e2495735f7b4c0c49e8a029f to your computer and use it in GitHub Desktop.
Selection Sort JS
function selectionSort(arr) {
let currentMin = 0;
let newMin = 0;
let complete = false;
while (!complete) {
for (var i = currentMin; i < arr.length; i++) {
if(arr[i] < arr[newMin]) {
newMin = i;
}
}
if (newMin !== currentMin) {
var temp = arr[currentMin]
arr[currentMin] = arr[newMin]
arr[newMin] = temp;
}
currentMin ++
newMin = currentMin
if (currentMin === arr.length-1) {
complete = true
}
}
return arr
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment