首页 > 代码库 > selectSort

selectSort

function selectSort(arr) {
    var len = arr.length;

    for (var i = 0; i < len -1; i++) {
        for (var j = i + 1; j < len; j++) {
            if (arr[i] > arr[j]) {
                var temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
    }

    return arr;
}

var arr = [1, 3, 2, 5, 4];
console.log(selectSort(arr)); // [1, 2, 3, 4, 5]

 

selectSort