【问题标题】:My implementation of merge sort is not working我的合并排序实现不起作用
【发布时间】:2015-05-03 22:34:07
【问题描述】:

此合并排序算法的实现失败,因为 ArrayIndexIsOut of bounds。

public static int[] mergeSort(int[] toBeSorted) {
    //If there is only one item in the array, and it is said to be sorted
    if (toBeSorted.length <= 1){
        return toBeSorted;
    }

    //find the indexes of the two sub-groups
    int[] left = new int[toBeSorted.length/2];
    int[] right = new int[toBeSorted.length-left.length];
    //Fill each sub-group with the correct numbers
    //Starting with the left group
    for(int i = 0; i <= left.length - 1; i++){
        left[i] = toBeSorted[i];
    }
    //Then the right group
    for(int i = left.length - 1; i <= toBeSorted.length - 1; i++){
        right[i] = toBeSorted[i];
    }


    //Merge sort each sub-group
    mergeSort(left);
    mergeSort(right);

    //Merge the two sub-groups
    toBeSorted = merge(left, right);

    return toBeSorted;
}

//Merging method
public static int[] merge(int[] left, int[] right){
    //Answer array
    int[] merged = new int[left.length + right.length];
    //Next index to check in each array
    int lCursor = 0;
    int rCursor = 0;
    //Next index to place numbers into answer
    int mergedCursor = 0; 

    //The merging part:
    //If there are still items to merge, then do so
    while(mergedCursor != merged.length){
        //left index is empty
        if(lCursor == left.length) {
            merged[mergedCursor] = right[rCursor];
            //increment the correct cursors
            rCursor += 1;
            mergedCursor += 1;
        }
        //right index is empty
        else if(rCursor == right.length) {
            merged[mergedCursor] = right[lCursor];
            //increment the correct cursors
            lCursor += 1;
            mergedCursor += 1;
        } 
        //Left side is smaller
        else if(left[lCursor]<right[rCursor]){
            merged[mergedCursor] = left[lCursor];
            //increment the correct cursors
            lCursor += 1;
            mergedCursor +=1;
        }
        //Right side is smaller
        else if(right[rCursor]<left[lCursor]){
            merged[mergedCursor] = right[rCursor];
            //increment the correct cursors
            rCursor += 1;
            mergedCursor +=1;
        }
    }
    //return the merged output
    return merged;
}

for 循环内将数字分配给正确数组的行是问题所在,但我不知道为什么。 另外,最初我在那个 for 循环中有 i = left.length,但这导致整个右数组被设置为零。

编辑:我将第二个 for 循环更改为:

  for(int i = 0; i <= right.length - 1; i++){
        right[i] = toBeSorted[i + left.length];
    }

现在正确的数组被正确填充了。

编辑 2:我修复了合并部分。由于某些奇怪的原因,当发现索引为空时,我仍然从空数组中取出。我还将其更改为增强的 for 循环以摆脱合并光标。新的合并方式如下:

    public static int[] merge(int[] left, int[] right){
    //Answer array
    int[] merged = new int[left.length + right.length];
    //Next index to check in each array
    int lCursor = 0;
    int rCursor = 0;


    //The merging part:
    //Keep going until output array is full
    for (int i = 0; i <= merged.length - 1; i++) {
        //left index is empty
        if(lCursor == left.length) {
            merged[i] = right[rCursor];
            //increment the correct cursor
            rCursor += 1;
        }
        //right index is empty
        else if(rCursor == right.length) {
            merged[i] = left[lCursor];
            //increment the correct cursor
            lCursor += 1;
        } 
        //Left side is smaller
        else if(left[lCursor]<right[rCursor]){
            merged[i] = left[lCursor];
            //increment the correct cursor
            lCursor += 1;
        }
        //Right side is smaller
        else if(right[rCursor]<left[lCursor]){
            merged[i] = right[rCursor];
            //increment the correct cursor
            rCursor += 1;
        }
    }
    //return the merged output
    return merged;
}

【问题讨论】:

  • mergeSort 返回一个新数组(在merge 中创建),但在其中你在左右调用它并丢弃结果。
  • 这并不能解决数组索引第一次越界的问题。
  • right[lCursor] 看起来也很奇怪。您没有可以调试的 IDE?

标签: java recursion mergesort


【解决方案1】:

在第二个 for 循环中,您从 (left.Length - 1) 开始 (i)。你想要的是

for (int i = right.Length, j = 0; (j <= right.Length - 1) && (i <= toBeSorted.Length - 1); i++, j++)
{
    right[j] = toBeSorted[i];
}

此外,结束第一个循环的值 (left.Length - 1) 与开始下一个循环的值相同。这意味着您在两个(左和右)数组中都获得了中间值。但这可能不是你想要的。所以我把它改成了 (right.Length) 而不是 (right.Length - 1)。

【讨论】:

  • 我更改了第二个 For 循环以使其正常工作,但不是按照您建议的方式。通过在 for 循环之后立即放置 System.out.println("Right:" + Arrays.toString(right));,我可以看到,正确的数组包含正确的值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-23
  • 2019-07-31
  • 2016-08-19
  • 1970-01-01
  • 2017-01-13
相关资源
最近更新 更多