SOURCE

function QuickSort(arr) {
    qs(arr, 0, arr.length - 1);
}

function qs(arr, l, r) {
    if (l >= r) return;
    const index = Partition(arr, l, r);
    qs(arr, l, index - 1);
    qs(arr, index + 1, r);
}

function Partition(arr, l, r) {
    let low = l;
    let high = r;
    const pivot = arr[low];

    while (low < high) {
        while (low < high && arr[high] >= pivot) high --;
        arr[low] = arr[high];
        while (low < high && arr[low] <= pivot) low ++;
        arr[high] = arr[low];
    }
    arr[low] = pivot;
    return low;
}

let newArr = [21, 34, 74, 3, 20, 2, 56, 46, 6, 1];
QuickSort(newArr);
console.log(newArr);
console 命令行工具 X clear

                    
>
console