function quickSort(array) {
    quick(array, 0, array.length - 1);
}

function quick(array, left, right) {
    let index = 0;
    if (array.length > 1) {
        index = partition(array, left, right);

        if (left < index - 1) {
            quick(array, left, index - 1);
        }

        if (index < right) {
            quick(array, index, right);
        }
    }
}

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

    while (i <= j) {
        while (array[i] < pivot) {
            i++;
        }

        while (array[j] > pivot) {
            j--;
        }

        if (i <= j) {
            [array[i], array[j]] = [array[j], array[i]]
            i++;
            j--;
        }
    }

    return i;
}

后脑勺
23 声望1 粉丝

一只有追求、有梦想、喜欢写出漂亮代码的程序猿儿