算法-快速排序(优雅版)

static void quicksort(Integer n[], int left, int right) {
    //对特殊值和边界值的检查
    if (n == null || left < 0) {
        return;
    }
    int dp;
    if (left < right) {
        dp = partition(n, left, right);
        quicksort(n, left, dp - 1);
        quicksort(n, dp + 1, right);
    }
}

static int partition(Integer n[], int left, int right) {
    int pivot = n[left];
    while (left < right) {
        while (left < right && n[right] >= pivot)
            right--;
        if (left < right)
            n[left++] = n[right];
        while (left < right && n[left] <= pivot)
            left++;
        if (left < right)
            n[right--] = n[left];
    }
    n[left] = pivot;
    return left;
}
posted @ 2017-09-13 22:33  舒山  阅读(331)  评论(2编辑  收藏  举报