快速排序

const arr = [21, 2, 5, 3, 1, 2, 6, 3, 3];

function quick(data) {
    if (data.length <= 1) {
        return data;
    }
    const base = data[0];
    const left = [],
        right = [];
    for (var i = 1; i < data.length; i++) {
        if (data[i] < base) {
            left.push(data[i]);
        } else {
            right.push(data[i]);
        }
    }
    return quick(left).concat(base, quick(right));
}

const res = quick(arr)
console.log(res);

 

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