Selection Sort

Tuesday, June 8, 2010 by gradbot

The next set of sorts are based on selection sort. Selection sort works by finding the minimal value in a list and swapping it with the first position. This is repeated with the remainder of the list swapping the next minimal value with the second position and so forth until the list is sorted. Wikipedia on Selection sort.

algorithms.sort.selection = function(array, predicate) {
    var length = array.length;
    for (var start = 0; start < length; start++) {
        var min = start;
        for (var i = start; i < length; i++) {
            if (predicate(array, i, min)) {
                min = i;
        array.swap(start, min);  

Filed under , , , having