Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的合并排序实现在Java中出错?_Java_Algorithm - Fatal编程技术网

为什么我的合并排序实现在Java中出错?

为什么我的合并排序实现在Java中出错?,java,algorithm,Java,Algorithm,我正在同时学习Java和算法。我在这个类中实现了合并排序 public class Sorter { private void merge(int [] numbers, int low, int mid, int high) { // create a new array that will contain the merged integers int[] arrIntMerged = new int[high - low + 1];

我正在同时学习Java和算法。我在这个类中实现了合并排序

public class Sorter {
    private void merge(int [] numbers, int low, int mid, int high) {
            // create a new array that will contain the merged integers
            int[] arrIntMerged = new int[high - low + 1];

            // set indices
            int i = low, j = mid + 1, k = 0;

            // add the lesser integer into merged array
            while (i <= mid && j <= high) {
                if (numbers[i] < numbers[j]) {
                    arrIntMerged[k] = numbers[i];
                    i++;
                } else {
                    arrIntMerged[k] = numbers[j];
                    j++;
                }
                k++;
            }

            // add anything left in the left side of the array
            while (i <= mid) {
                arrIntMerged[k] = numbers[i];
                i++;
                k++;
            }

            // add anything left in the right side of the array
            while (j <= high) {
                arrIntMerged[k] = numbers[j];
                j++;
                k++;
            }

            // write this newly created array into the positions in the original array
            for (int l = 0; l < arrIntMerged.length; l++) {
                numbers[l + low] = arrIntMerged[l];
            }
        }

        // recursive implementation
        private void _mergeSort(int[] numbers, int low, int high) {
            if (low == high)
                return;
            else {
                // find midpoint while preventing overflow
                int mid = low + (high - low) / 2;
                // sort left and right side
                _mergeSort(numbers, low, mid);
                _mergeSort(numbers, mid + 1, high);
                // merge both sides
                merge(numbers, low, mid + 1, high);
            }
        }

        // friendly interface to begin merge sort
        public void mergeSort(int[] numbers) {
            _mergeSort(numbers, 0, numbers.length - 1);
        }
}

不幸的是,标准输出读取的是
[2,3,4,5,6,7,8,1]
,这是错误的。为什么我的合并排序出错?我非常确定我在
\u mergeSort
中的边界条件,因此我怀疑我的
merge
函数有误。

您的问题来自
merge
中的以下赋值:

j = mid + 1
mid
已经是合并右侧第一个数字的索引,此增量使合并逻辑的其余部分从错误的数组位置开始


因为看起来您正在学习,我不会通过发布所需的实际代码更改来破坏您的体验,但这里有一个提示:检查所有与
mid
值进行比较的地方。

当您使用调试器进行调试时,发生了什么事?您可能有一个++在错误的行中,因为1结束于排序数组的末尾。然而,我们不是在这里调试您的代码以找到那一行。享受调试的乐趣:)嗯,好的:)我想这是一个学习调试器的好机会。我会和你玩得开心的谢谢!我想我得到了。对,mid已经是右边的第一个索引。感谢您没有直接调试它:)
j = mid + 1