Javascript Selection Sort

by stevenkaspar

JavaScript

let selectionSort = (items) => {  
    var length = items.length;
    for (var i = 0; i < length - 1; i++) {
        //Number of passes
        var min = i; //min holds the current minimum number position for each pass; i holds the Initial min number
        for (var j = i + 1; j < length; j++) { //Note that j = i + 1 as we only need to go through unsorted array
            if (items[j] < items[min]) { //Compare the numbers
                min = j; //Change the current min number position if a smaller num is found
            }
        }
        if (min != i) {
            //After each pass, if the current min num != initial min num, exchange the position.
            //Swap the numbers 
            items[i] = items.splice(min, 1, items[i])[0];
        }
    }
    return items
}

console.log(selectionSort([4,3,1,5,74,6,4,9]))