编辑代码

/**
 * 数组排序
*/
let arr = [1, 6,20, 20, 3, 2, 34, 12, 5, 4,1];

// 冒泡算法
(function getTarArr(arr) {
    let tmp = '';
    for (let i = 0; i< arr.length -1; i++) {
        for (let j = 0; j < arr.length - 1 -i; j++) {
            if (arr[j] > arr[j+1]) {
                tmp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = tmp; 
            }
        }
    }
    console.log(arr);
})(arr.slice()) 


// 二分法(快速排序)
function sortArr(arr) {
    if (arr.length <= 1 || (arr.length === 2 && arr[0] === arr[1])) {
        return arr;
    }
    let middleIndex = Math.floor(arr.length / 2);
    let middleVal = arr[middleIndex];
    let leftArr = [], rightArr = [];
    
    for (let i = 0; i< arr.length; i++) {
        if (arr[i] > middleVal)  rightArr.push(arr[i]);
        else if(arr[i] < middleVal) leftArr.push(arr[i]);
    }
    return sortArr(leftArr).concat([middleVal]).concat(sortArr(rightArr));
}

console.log(sortArr(arr.slice()))