繁体   English   中英

我需要帮助对我的MergeSort和Merge代码进行故障排除

[英]I need help troubleshooting my code for MergeSort and Merge

因此,我一直在为算法项目研究MergeSort,但是在获取代码以对数组进行排序时遇到了各种问题。 每当我生成一个字符串并将其放入MergeSort时,它看起来就完全一样。 我需要一些帮助,以查找我的代码中的错误在哪里,为什么给我这个错误以及一个简单但很好的解释的解决方案。

这是我过去尝试过的方法:

  1. 我尝试使用return arr[0]而不是arr ,但是由于无法将int转换为int[]而使我出错。
  2. 我已经查看了我的合并类,那里似乎一切正常。
  3. 我已经与我的老师讨论了这个问题,他说一切看起来都很好,但是我知道某个地方一定有问题。
  4. 我试图删除return merge(arr1, arr2)但是系统抛出一个错误,告诉我必须返回一些东西。
  5. 我试图单独打印出数组,但是它仍然没有显示任何更改,并且与原始字符串完全相同。

合并方法:

private static int[] merge(int[] a, int[] b)
{
    int[] c = new int[a.length + b.length];
    int counterA = 0;
    int counterB = 0;
    int counterC = 0;

    while (counterA != a.length && counterB != b.length)
    {
      if (a[counterA] < b[counterB])
      {
        c[counterC] = a[counterA];
        counterA++;
        counterC++;
      }
      else
      {
        c[counterC] = b[counterB];
        counterB++;
        counterC++;
      }
    }

    while (counterB == b.length && counterA != a.length)
    {
      c[counterC] = a[counterA];
      counterA++;
      counterC++;
    }

    while (counterA == a.length && counterB != b.length)
    {
      c[counterC] = b[counterB];
      counterB++;
      counterC++;
    }

    return c;
}

MergeSort方法:

public static int[] mergeSort(int[] arr)
{ 
    if (arr.length == 1)
    {
      return arr[0];
    }

    int[] arr1 = new int[arr.length / 2];
    int[] arr2 = new int[arr.length - arr1.length];

    for (int i = 0; i < arr1.length; i++)
    {
      arr1[i] = arr[i];
    }

    for (int i = 0; i < arr2.length; i++)
    {
      arr2[i] = arr[i + arr1.length];
    }

    arr1 = mergeSort(arr1);
    arr2 = mergeSort(arr2);

    return merge(arr1, arr2);
}

由于该数组是随机生成的,因此示例如下:

9, 1, 7, 5, 7, 2, 2, 9, 8, 9

预期结果应为:

1, 2, 2, 5, 7, 7, 8, 9, 9, 9

但是,这是输出的结果(数组不变):

9, 1, 7, 5, 7, 2, 2, 9, 8, 9 

您的代码未按书面要求进行编译。 mergeSort ,如果数组长度等于1,则应return arr 进行此更改后,您的代码可以正常工作。

但是,我们可以对您的代码进行一些小的更改以使其更简洁。 请注意,对于您在merge最后一个while循环,检查counterA == a.length将始终为true。 因此,我们可以将其删除。 同样,在访问数组时可以完成递增计数器。 合并这些建议将为您的merge方法生成以下代码:

private static int[] merge(int[] a, int[] b)
{
    int[] c = new int[a.length + b.length];
    int counterA = 0;
    int counterB = 0;
    int counterC = 0;

    while(counterA != a.length && counterB != b.length)
    {
        if(a[counterA] < b[counterB])
            c[counterC++] = a[counterA++];
        else
            c[counterC++] = b[counterB++];
    }

    while(counterB == b.length && counterA != a.length)
        c[counterC++] = a[counterA++];

    while(counterB != b.length)
        c[counterC++] = b[counterB++];

    return c;
}

我在代码中看到的唯一问题是return arr[0]; 而不是return arr; return new int[]{arr[0]}; 在mergeSort中。

除此之外,代码按预期工作。

如果您没有看到正确的输出,则可能是您使用的输出不正确。

这是我如何测试的示例。

    public static void main(String[] args) {

        int[] input = new int[]{9, 1, 7, 5, 7, 2, 2, 9, 8, 9};
        int[] output = mergeSort(input);
    }

您的代码中有2个问题:

  • 您返回数组元素arr[0]而不是数组本身arr
  • 您不处理arr.length == 0的情况。 它也应该返回arr

请注意,可以简化代码:

  • 您可以在复制值时对索引值使用++运算符,
  • 您应该在第二和第三个while循环中删除冗余测试。

这是一个改进的版本:

private static int[] merge(int[] a, int[] b)
{
    int[] c = new int[a.length + b.length];
    int counterA = 0;
    int counterB = 0;
    int counterC = 0;

    while (counterA != a.length && counterB != b.length)
    {
        if (a[counterA] <= b[counterB])
            c[counterC++] = a[counterA++];
        else
            c[counterC++] = b[counterB++];
    }

    while (counterA != a.length)
        c[counterC++] = a[counterA++];

    while (counterB != b.length)
        c[counterC++] = b[counterB++];

    return c;
}

public static int[] mergeSort(int[] arr)
{ 
    if (arr.length < 2)
        return arr;

    int[] arr1 = new int[arr.length / 2];
    int[] arr2 = new int[arr.length - arr1.length];

    for (int i = 0; i < arr1.length; i++)
        arr1[i] = arr[i];

    for (int i = 0; i < arr2.length; i++)
        arr2[i] = arr[i + arr1.length];

    arr1 = mergeSort(arr1);
    arr2 = mergeSort(arr2);

    return merge(arr1, arr2);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM