Created
August 29, 2018 13:32
-
-
Save oliverbth05/73eaaf09e2495735f7b4c0c49e8a029f to your computer and use it in GitHub Desktop.
Selection Sort JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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