执行 dualPivotQuickSort 时出现 StackOverflow 错误

问题描述

我正在使用以下链接的 dualPivotQuickSort 算法。一切都很好,直到数组大小具有 100.000 索引。当我将大小设置为 100.000 并在数组中放入递增或递减的数字时,我的程序崩溃并出现 StackOverflow 错误。使用随机数时没有问题,即使是 100.000 长度的数组。 https://cs.stackexchange.com/questions/24092/dual-pivot-quicksort-reference-implementation

    void sort(int[] A,int left,int right) {
    if (right > left) {
        // Choose outermost elements as pivots
        if (A[left] > A[right]) swap(A,left,right);
        int p = A[left],q = A[right];

        // Partition A according to invariant below
        int l = left + 1,g = right - 1,k = l;
        while (k <= g) {
            if (A[k] < p) {
                swap(A,k,l);
                ++l;
            } else if (A[k] >= q) {
                while (A[g] > q && k < g) --g;
                swap(A,g);
                --g;
                if (A[k] < p) {
                    swap(A,l);
                    ++l;
                }
            }
            ++k;
        }
        --l; ++g;

        // Swap pivots to final place
        swap(A,l); swap(A,right,g);

        // Recursively sort partitions
        sort(A,l - 1);
        sort(A,l + 1,g - 1);
        sort(A,g + 1,right);
    }
}

   void swap(int[] A,int i,int j) {
    final int tmp = A[i]; A[i] = A[j]; A[j] = tmp;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)