SOURCE

let testArr = [5, 17, 15, 5, 7, 33, 2, 6, 20];
document.write(`Original Array is [${testArr}]<br/>`);

function swap(arr, left, right) {
    let temp = arr[left];
    arr[left] = arr[right];
    arr[right] = temp;
}
function partition(arr, left, right) {
    let pivot = arr[Math.floor((left + right) / 2)];
    let i = left;
    let j = right;

    while (i <= j) {
        while (arr[i] < pivot) {
            i++;
        }
        while (arr[j] > pivot) {
            j--;
        }
        if (i <= j) {
            swap(arr, i, j);
            i++;
            j--;
        }
    }
    return i;
}

function quickSort(arr, left, right) {
    let pivotIndex;
    if (arr.length <= 1) {
        return arr;
    }
    else {
        pivotIndex = partition(arr, left, right);
        if (left < pivotIndex-1) {
            quickSort(arr, left, pivotIndex-1);
        }
        if (pivotIndex < right) {
            quickSort(arr, pivotIndex, right);
        }
    }
    return arr;
}


let resultArr = quickSort(testArr, 0, testArr.length-1);
document.write(`Ordered Array is[${resultArr}]`);
console 命令行工具 X clear

                    
>
console