SOURCE

function quickSort(arr) {
    // 递归的出口:当数组的长度为1的时候,我们认为这个数组已经是有序的了
    if (arr.length <= 1) {
        return arr;
    }
    // 取中间点作为基数
    const pivot = Math.floor(arr.length / 2);
    const pivotNumber = arr[pivot];
    const leftArr = [];
    const rightArr = [];
    for (let i = 0; i < arr.length; i++) {
        if (i !== pivot) {
            arr[i] < pivotNumber ? leftArr.push(arr[i]) : rightArr.push(arr[i]);
        }
    }
    return [...quickSort(leftArr), pivotNumber, ...quickSort(rightArr)]
}

console.log('[2, 9, 6, 7, 4, 3, 1, 8]', quickSort([2, 9, 6, 7, 4, 3, 1, 8]))
console 命令行工具 X clear

                    
>
console