SOURCE

//选择排序,将小的放在最前面
function selectionSort(arr) {
    let len
    if(arr && arr.length > 0){
        len = arr.length
    }else{
        return []
    }
    for(let out = 0; out < len - 1; out++){
        let minIdx = out
        //循环得到最小的minIndex
        for(let inner = out + 1; inner < len; inner++){
            minIdx = arr[inner] < arr[minIdx] ? inner : minIdx
        }
        //交换数据
        swap(arr, out, minIdx)
    }
    return arr
}
function swap(arr, out, minIdx){
    let temp = arr[out];
    arr[out] = arr[minIdx];
    arr[minIdx] = temp;
}
console.log(selectionSort([3,5,1,7,2,4]))
console 命令行工具 X clear

                    
>
console