编辑代码

function quickSort(arr, low = 0, high = arr.length - 1) {
    if (low < high) {
        const pivotIndex = partition(arr, low, high)
        quickSort(arr, low, pivotIndex - 1)
        quickSort(arr, pivotIndex + 1, high)
    }
}

function partition(arr, low, high) {
    const pivot = arr[high]
    let i = low - 1;

    for (let j = low; j < high; j++) {
        if (arr[j] <= pivot) {
            i++
            swap(arr, i, j)
        }
    }

    swap(arr, i + 1, high)
    return i + 1
}

function swap(arr, i, j) {
    const temp = arr[i]
    arr[i] = arr[j]
    arr[j] = temp
}

const arr1 = [9, 7, 1, 2, 5]
const arr2 = [-1,0,0,3]
const arr3 = [200,1,3,99,-200]
quickSort(arr1)
quickSort(arr2)
quickSort(arr3)
console.log(arr1)
console.log(arr2)
console.log(arr3)