简体   繁体   English

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

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

So I've been working on MergeSort for an Algorithm project, but I've ran into various problems when it comes to getting the code to sort the arrays. 因此,我一直在为算法项目研究MergeSort,但是在获取代码以对数组进行排序时遇到了各种问题。 Whenever I generate a string and put it into MergeSort, it seems to just come out exactly the same. 每当我生成一个字符串并将其放入MergeSort时,它看起来就完全一样。 I want some help in finding where the mistake in my code is, why is it giving me this, and a solution with a simple, but good explanation. 我需要一些帮助,以查找我的代码中的错误在哪里,为什么给我这个错误以及一个简单但很好的解释的解决方案。

Here's what I've tried in the past: 这是我过去尝试过的方法:

  1. I've tried to use return arr[0] instead of arr , but it throws me an error with it being unable to convert from int to int[] . 我尝试使用return arr[0]而不是arr ,但是由于无法将int转换为int[]而使我出错。
  2. I've looked in my merge class and everything seems to be ok there. 我已经查看了我的合并类,那里似乎一切正常。
  3. I've discussed the issue with my teacher and he says that everything looks fine, but I know that there must be something wrong somewhere. 我已经与我的老师讨论了这个问题,他说一切看起来都很好,但是我知道某个地方一定有问题。
  4. I've tried to remove return merge(arr1, arr2) but the system throws an error telling me I have to return something. 我试图删除return merge(arr1, arr2)但是系统抛出一个错误,告诉我必须返回一些东西。
  5. I've tried to print out the arrays individually, but it still shows no changes and is the exact same as the original string. 我试图单独打印出数组,但是它仍然没有显示任何更改,并且与原始字符串完全相同。

Merge method: 合并方法:

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 method: 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);
}

Because the array is randomly generated, an example would be this: 由于该数组是随机生成的,因此示例如下:

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

The intended result should be this: 预期结果应为:

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

However, this is what the output is instead (The array comes out unchanged): 但是,这是输出的结果(数组不变):

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

Your code doesn't compile as written. 您的代码未按书面要求进行编译。 In mergeSort , if the array length is equal to 1, it should return arr . mergeSort ,如果数组长度等于1,则应return arr After this change, your code works fine. 进行此更改后,您的代码可以正常工作。

We can, however, make a few small changes to your code to make it cleaner. 但是,我们可以对您的代码进行一些小的更改以使其更简洁。 Notice that for your last while loop in merge , the check counterA == a.length will always be true. 请注意,对于您在merge最后一个while循环,检查counterA == a.length将始终为true。 Therefore, we can remove it. 因此,我们可以将其删除。 Also, incrementing counters can be done while accessing the array. 同样,在访问数组时可以完成递增计数器。 Combining these suggestions results in the following code for your merge method: 合并这些建议将为您的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;
}

The only problem I see in the code is the return arr[0]; 我在代码中看到的唯一问题是return arr[0]; instead of return arr; 而不是return arr; or return new int[]{arr[0]}; return new int[]{arr[0]}; in mergeSort. 在mergeSort中。

Aside from that the code works as expected. 除此之外,代码按预期工作。

If you are not seeing the correct output, you probably aren't using the output correctly. 如果您没有看到正确的输出,则可能是您使用的输出不正确。

Here is an example of how I tested it. 这是我如何测试的示例。

    public static void main(String[] args) {

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

There are 2 problems in your code: 您的代码中有2个问题:

  • you return an array element arr[0] instead of the array itself arr 您返回数组元素arr[0]而不是数组本身arr
  • you do not handle the case of arr.length == 0 . 您不处理arr.length == 0的情况。 It should return arr as well. 它也应该返回arr

Note that the code can be simplified: 请注意,可以简化代码:

  • you can use the ++ operator on index values when copying the values, 您可以在复制值时对索引值使用++运算符,
  • you should remove the redundant tests on the second and third while loops. 您应该在第二和第三个while循环中删除冗余测试。

Here is an improved version: 这是一个改进的版本:

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