michaelp0730
1/4/2015 - 6:38 PM

Selection Sort JavaScript

function swap(items, firstIndex, secondIndex){
    var temp = items[firstIndex];
    items[firstIndex] = items[secondIndex];
    items[secondIndex] = temp;
}

function selectionSort(items){

    var len = items.length,
        min;

    for (i=0; i < len; i++){

        //set minimum to this position
        min = i;

        //check the rest of the array to see if anything is smaller
        for (j=i+1; j < len; j++){
            if (items[j] < items[min]){
                min = j;
            }
        }

        //if the minimum isn't in the position, swap it
        if (i != min){
            swap(items, i, min);
        }
    }

    return items;
}

var names = ['Michael', 'Jessica', 'Ava', 'Elise', 'Enzo'];
var numbers = [44, 57, 89, 23, 77, 90, 1, 14];
var letters = ['c', 'g', 'e', 'R', 'E', 'z', 'q', 'A'];