简体   繁体   English

无法弄清楚我的合并排序代码有什么问题

[英]Can't figure out what is wrong with my merge sort code

I went over it multiple times and I am not sure what I am doing wrong.我看了很多遍,我不确定我做错了什么。 he logic seems OK but it is printing out the first number only.他的逻辑似乎没问题,但它只打印出第一个数字。 I left out the main method:我省略了主要方法:

public class MergeSort {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] bob = {4,7,99,8,12,6};

        Mergesort(bob);

        for(int i=0; i< bob.length; i++){
            System.out.print(bob[i]+",");
        }
    }

    public static int[] Mergesort(int[] bob){

        int length = bob.length;
        int mid = bob.length/2;
        if(length <= 1){
            return bob;
        }

        int[] left = new int[mid];
        int[] right = new int[length-left.length];

        for(int i=0; i< mid; i++){
            left[i]=bob[i];
        }
        for(int j=mid; j< length; j++){
            right[j-mid]=bob[j];
        }

        Mergesort(left);
        Mergesort(right);
        merge(left,right, bob);
        return bob;
    }

    //this is merge method
    public static int[] merge(int[] left, int[] right, int[] bob){
        int l= left.length;
        int r = right.length;

        int i=0, j=0,k=0;

        while(i<l && j<r){
            if(left[i]<=right[j]){
                bob[k] = left[i];
                i++; 

            }
            else
            {
                bob[k] = right[j];
                j++;

            }
            k++;
        }

        while(i<l){
            bob[k] = left[i];
            i++;
            k++;
        }
        while(j<r){
            bob[k] = bob[j];
            j++;
            k++;
        }

        return bob;

    }

}

I believe your bug is in this line in merge() :我相信您的错误在merge()中的这一行:

        bob[k] = bob[j];

It should be它应该是

        bob[k] = right[j];

With this change I get this output:通过此更改,我得到了以下输出:

4,6,7,8,12,99,

Rather than just tell you the answer, I'm going to show you how to quickly figure it out via test/debug.我将向您展示如何通过测试/调试快速找出答案,而不仅仅是告诉您答案。 You know, "teach a man to fish" and all that ( source ).你知道,“教人钓鱼”等等( 来源)。

So, let's test your merge method.因此,让我们测试您的merge方法。 I've modified the main method to only merge two sorted arrays {1, 3, 5} and {2, 4, 6} .我修改了 main 方法,只合并两个排序的数组{1, 3, 5}{2, 4, 6} We know that the output should be {1, 2, 3, 4, 5, 6} .我们知道输出应该是{1, 2, 3, 4, 5, 6}

At the same time, let's debug as well.同时,让我们调试一下。 The most primitive way to debug is known as "printf debugging," meaning we just put lots of logging statements in the code so that we can see what the internal state is at every step.最原始的调试方式被称为“printf 调试”,这意味着我们只是在代码中放置了大量日志语句,以便我们可以看到每一步的内部状态。 I've added logging wherever your merge output array is modified.我在修改merge输出数组的地方添加了日志记录。 The goal is to observe the moment it is modified in an incorrect/unexpected way.目标是观察以不正确/意外方式修改的那一刻。

Here's the code with these two modifications:这是带有这两个修改的代码:

public class MergeSort {

  public static void main(String[] args) {
    int[] left = {1, 3, 5};
    int[] right = {2, 4, 6};
    int[] merged = {0, 0, 0, 0, 0, 0};
    merge(left, right, merged);
  }

  public static int[] merge(int[] left, int[] right, int[] bob) {
    System.out.print("Before merge: ");
    System.out.println(Arrays.toString(bob));
    int l = left.length;
    int r = right.length;

    int i = 0, j = 0, k = 0;

    while (i < l && j < r) {
      if (left[i] <= right[j]) {
        bob[k] = left[i];
        System.out.print("Merge step 1: ");
        System.out.println(Arrays.toString(bob));
        i++;

      } else {
        bob[k] = right[j];
        System.out.print("Merge step 2: ");
        System.out.println(Arrays.toString(bob));
        j++;

      }
      k++;
    }

    while (i < l) {
      bob[k] = left[i];
      System.out.print("Merge step 3: ");
      System.out.println(Arrays.toString(bob));
      i++;
      k++;
    }
    while (j < r) {
      bob[k] = bob[j];
      System.out.print("Merge step 4: ");
      System.out.println(Arrays.toString(bob));
      j++;
      k++;
    }

    return bob;
  }
}

And here is the output:这是输出:

Before merge: [0, 0, 0, 0, 0, 0]
Merge step 1: [1, 0, 0, 0, 0, 0]
Merge step 2: [1, 2, 0, 0, 0, 0]
Merge step 1: [1, 2, 3, 0, 0, 0]
Merge step 2: [1, 2, 3, 4, 0, 0]
Merge step 1: [1, 2, 3, 4, 5, 0]
Merge step 4: [1, 2, 3, 4, 5, 3]

So, where is the problem in your code?那么,您的代码中的问题在哪里? Clearly in "merge step 4," or the line bob[k] = bob[j];显然在“合并步骤 4”中,或者行bob[k] = bob[j]; . . How cool is that?多么酷啊? With a few simple print calls, we've located the exact line of your bug!通过几个简单的print调用,我们已经找到了您的错误的确切行!

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

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