3

首先,我要声明这是一个家庭作业问题,我已经进行了大量的尝试。

我被要求修改 Java 中的快速排序,以使用公式将枢轴设置为数组中 9 个值的伪中位数i * (n-1) /8

我写了一个computeMedian方法,它接受 3 个整数,确定最大值,然后返回那个值。

编码:

public static int computeMedian(int x, int y, int z)
    {
        if((x >= y && x <= z) || (x >= z && x <= y)) {return x;}
        else if((y >= x && y <= z) || (y >= z && y <= x)) {return y;}
        else if((z >= x && z <= y) || (z >= y && z <= x)) {return z;}
        else { return 0; }
    }

然后我在我的findPivot方法中使用它,该方法采用当前array, from, to值并使用它们来构造一个枢轴

这是该代码:

public static int findPivot(int[] a, int from, int to)
    {
        if(a.length <= 7)
        {
            return a[(to)/2];
        }
        else if(a.length > 7 && a.length <= 40)
        {
            return computeMedian(a[from], a[(to)/2] , a[to]);
        }
        else
        {
            int x = computeMedian(a[0 * (to) / 8], a[1 * (to) / 8], a[2 * (to) / 8]);
            int y = computeMedian(a[3 * (to) / 8], a[4 * (to) / 8], a[5 * (to) / 8]);
            int z = computeMedian(a[6 * (to) / 8], a[7 * (to) / 8], a[8 * (to) / 8]);
            return computeMedian(x,y,z);
        }
    }

此方法适用于对任何小于或等于 40 的数组进行排序,但一旦它大于 40,我就会收到堆栈溢出错误,导致返回computeMedianelse {}部分中的方法。如果我把它放在那里,我会注意到它return computeMedian(a[from], a[(to)/2] , a[to]);在 > 40 部分有效,但这只是 3 个值的中值,而不是 3 组中值的中值。

目前,这就是我findPivot插入快速排序分区方法的方式:

private static int modPartition(int[] a, int from, int to)
    {
        int pivot = findPivot(a, from, to);
        int i = from - 1;
        int j = to + 1;
        while(i < j)
        {
            i++; while (a[i] < pivot) { i++; }
            j--; while (a[j] > pivot) { j--; }
            if (i < j) { swap(a, i, j); }
        }
        return j;
    }

computeMedian我对为什么我的方法无法在更大的数据集上工作感到非常困惑。我尝试i * (n-1) / 8通过 for 循环将值放入数组中,对它们进行排序并在中间返回值,以及将值放入数组中p并调用computeMedian(computeMedian(p[0], p[1], p[2]), computeMedian(p[3],p[4],p[5]),...etc,我得到了相同的堆栈溢出问题,但它往往会移动到我的代码的不同部分并引导我转圈。

如果有人需要,我可以发布更多片段,但我认为我的问题可能就在这里。

谢谢您的帮助。我仍在学习,我认为掌握这一点完全可以帮助我自己解决未来的问题。

以下是堆栈跟踪中的问题行: 第 16 行:int p = modPartition(a, from, to); 第 18modSort(a, p+1, to); 行 第 23 行int pivot = findPivot(a, from, to);

这也是我的整个 modSort 方法:

public static void modSort(int[]a, int from, int to)
    {
        if(from >= to) { return; }
        int p = modPartition(a, from, to);
        modSort(a, from, p);
        modSort(a, p+1, to);
    }
4

2 回答 2

1

转载和更正

添加代码以重现错误...

private static void swap(int[] a, int i, int j) {
    int tmp = a[i];
    a[i] = a[j];
    a[j] = tmp;
}

public static void main(String[] args) {
    // Generate a sample
//      ArrayList<Integer> list = new ArrayList<>(64);
//      for (int i = 0; i < 64; i++) list.add(i);
//      Collections.shuffle(list);
//      System.out.println(list);
    int[] arr = {40, 9, 2, 62, 8, 42, 46, 23, 61, 45, 63, 48, 43, 36, 33, 32, 1, 55, 7, 17, 16, 25, 5, 26, 22, 11, 56, 38, 60, 31, 58, 29, 51, 34, 24, 54, 4, 3, 30, 20, 57, 18, 50, 44, 41, 12, 59, 6, 53, 39, 37, 35, 28, 13, 14, 15, 0, 19, 49, 52, 21, 27, 47, 10};

    modSort(arr, 0, arr.length-1);

    System.out.println(Arrays.toString(arr));
}

调试。设置断点StackOverFlowError(如评论中所建议)不起作用。所以我在行(开始modSort)处寻找一个常规断点。

对于此示例数据,开始modSort使用进行无限递归from=3;to=5。对于该范围,枢轴 p = 2,这似乎是不正常的。

我责怪findPivot(a,from,to)方法。看起来很适合为整体寻找支点a,但不适用于范围。尝试这个更正:

public static int findPivot(int[] a, int from, int to) {
    final int rangeLength = to - from + 1;
    if(rangeLength <= 7) {
        return a[(from + to)/2];
    } else if(rangeLength  <= 40) { // why test "a.length > 7" ?
        return computeMedian(a[from], a[(from + to)/2] , a[to]);
    } else {
        final int rangeLength_8 = (to - from) / 8;
        int x = computeMedian(a[from], a[from + rangeLength_8], a[from + 2 * rangeLength_8]);
        int y = computeMedian(a[from + 3 * rangeLength_8], a[from + 4 * rangeLength_8], a[from + 5 * rangeLength_8]);
        int z = computeMedian(a[from + 6 * rangeLength_8], a[from + 7 * rangeLength_8], a[to]);
        return computeMedian(x,y,z);
    }
}

然后它适用于我的示例。我在这一点上停止它(必须得到一些睡眠)。

我认为您应该尝试熟悉调试器。我认为你应该更容易弄清楚。

于 2015-09-07T00:25:19.997 回答
0

现在您实际上已经包含了堆栈溢出问题的代码和错误消息,我们可以为您提供帮助。

从您的堆栈跟踪中,我们可以看到无限递归可能是对 的第二次调用modSort,因为第 18 行重复了。

由于该调用和传入参数之间的唯一区别是第二个参数,我敢打赌p小于from.

确认这一点的最佳方法是插入一个好的老式print语句。

public static void modSort(int[]a, int from, int to)
{
    if(from >= to) { return; }
    int p = modPartition(a, from, to);
    System.out.println("from=" + from + ", to=" + to + ", p=" + p);
    modSort(a, from, p);
    modSort(a, p+1, to);
}

生成的输出应该显示出非常清晰的错误模式。

于 2015-09-07T00:21:40.253 回答