var arr = [4, 1, 6, 9, 3, 5, 8, 7];

function swap(arr, a, b) {
    var tmp = arr[a];
    arr[a] = arr[b];
    arr[b] = tmp;
}

function quickSort(arr, begin, end) {
    if (begin >= end - 1) {
        return;
    }
    var left = begin;
    var right = end;
    do {
        do {
            left++;
        } while (left < right && arr[left] < arr[begin]);

        do {
            right--;
        } while (right > left && arr[right] > arr[begin]);


        if (left < right) {
            swap(arr, left, right);
        }

    } while (left < right)
    var swapPoint = left == right ? right - 1 : right;
    swap(arr, begin, swapPoint);
    quickSort(arr, begin, swapPoint);
    quickSort(arr, swapPoint + 1, end);
}

function sort(arr) {
    quickSort(arr, 0, arr.length);
}

sort(arr);
console.log(arr);

其他内容