选择排序

const arr = [21, 2, 5, 3, 1, 2, 6, 3, 3];
function select(data) {
    var index;
    for (let i = 0; i < data.length - 1; i++) {
        index = i;
        for (let m = i; m < data.length; m++) {
            if (data[index] > data[m]) {
                index = m;
            }
        }
        let tmp = data[index];
        data[index] = data[i];
        data[i] = tmp;
    }
    return data;
}
console.log(select(arr));

 

posted @ 2020-09-11 23:02  潴哥  阅读(105)  评论(0编辑  收藏  举报