Open lpatmo opened 5 years ago
function selectionSort(input){
//find smallest element
//swap with first element (or put at beginning)
//loop through rest of array
//find smallest element again; then swap with first unsorted
const swap = (arr, i,j) => {
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
const findMin = function(index) {
let minIndex = index;
for (let i=index; i < input.length; i++) {
if (input[i] < input[minIndex]) {
minIndex = i;
}
}
return minIndex;
}
for (let i = 0; i < input.length; i++) {
swap(input, findMin(i), i)
}
return input;
}
Implement selection sort.