简体   繁体   English

在java中合并两个排序的数组

[英]Merging two sorted arrays in java

public static int[] merge(int arr1[], int arr2[]) {
    int x = arr1.length + arr2.length;
    int[] arr3= new int[x];
    x=0;
    int i =0, j=0;
    while ( i<arr1.length||j<arr2.length)
    {
        if(arr1[i]<arr2[j])
        {
            arr3[x]= arr1[i];
            i++;
            x++;
        }
        else 
        {
            arr3[x]=arr2[j];
            j++;
            x++;
        }
        
    }
    return arr3;
}

Can anyone explain what is wrong in this program?谁能解释一下这个程序有什么问题?

And how to merge array if one is null array?如果一个是空数组,如何合并数组?

Example:-例子:-

Given Input: - array size for 1st = 5给定输入:-第一个数组大小 = 5

1 2 3 4 5 1 2 3 4 5

array size for 2nd = 0第二个数组大小 = 0

The short version:简短版本:

System.arraycopy(arr, 0, arr3, 0, arr.length);
System.arraycopy(arr2, 0, arr3, arr.length, arr2.length);
return Arrays.sort(arr3);

Implementing it yourself:自己实现:

Perhaps you should check if one of the arrays is null and also move the leftovers if some array is longer than the other.也许您应该检查其中一个数组是否为空,如果某个数组比另一个长,还应移动剩余部分。 Also maybe you should use && instead of ||也许你应该使用 && 而不是 || to avoid exception when checking if you reached the end of the array在检查是否到达数组末尾时避免异常

 public static int[] merge(int arr1[], int arr2[]) {
    // Checking for null arrays
    if (arr1 == null){
        return arr2;
    }
    if (arr2 == null){
        return arr1;
    }
    int x = arr1.length + arr2.length;
    int[] arr3= new int[x];
    x=0;
    int i =0, j=0;
    while (i<arr1.length && j<arr2.length) // This is where your exception occurs
    {
        if(arr1[i]<arr2[j])
        {
            arr3[x]= arr1[i];
            i++;
            x++;
        }
        else
        {
            arr3[x]=arr2[j];
            j++;
            x++;
        }

    }
    // Copying leftovers
    if (i < arr1.length){
        while(i < arr1.length){
            arr3[i + j] = arr1[i];
            i++;
        }
    }
    if (j < arr2.length){
        while (j < arr2.length){
            arr3[j + i] = arr2[j];
            j++;
        }
    }
    return arr3;
}

Lets make a dry run of your program.让我们试运行一下你的程序。 Suppose arr1 = {2} and arr2 = {1, 3}.假设 arr1 = {2} 和 arr2 = {1, 3}。

The declaration and initialization part are correct.声明和初始化部分是正确的。

    //All correct here
    int x = arr1.length + arr2.length;
    int[] arr3= new int[x];
    x=0;
    int i =0, j=0;

Now the control passes to the loop现在控制权传递给循环

    while (i < arr1.length || j < arr2.length)

The first condition is checked.检查第一个条件。

    if(arr1[i] < arr2[j])    //2 < 1 returns false

Program control passes to else and all the following statements are executed.程序控制传递给 else 并执行以下所有语句。

    arr3[x]=arr2[j];
    j++;
    x++;

Now the next time in the loop i = 0, j = 1, and x = 1. With that in mind again the if condition is checked.现在循环中的下一次 i = 0、j = 1 和 x = 1。再次记住这一点,检查 if 条件。 This time its 2 < 3. which returns true and the following part gets executed.这次它的 2 < 3. 返回 true 并执行以下部分。

    arr3[x]= arr1[i];
    i++;
    x++;

Now this time in the loop the value of i = 1, j = 1 and x = 2. Here is where the trouble begins.现在循环中的值 i = 1、j = 1 和 x = 2。这就是麻烦开始的地方。

    while (i < arr1.length || j < arr2.length) //1 < 1 || 1 < 2
                                               //returns true

The if condition is checked.检查 if 条件。

    if(arr1[i] < arr2[j])    //ArrayIndexOutOfBoundsException

This happened because you have to stop checking the condition when the value of i reaches its length.发生这种情况是因为当 i 的值达到其长度时,您必须停止检查条件。

Use this inside the while loop,在while循环中使用它,

    if (i == arr1.length)     //if all the elements of arr1 are already checked...copy arr2 leftover elements
        arr3[x] = arr2[j++];
    else if (j == arr2.length)//if all the elements of arr2 are already checked...copy arr1 leftover elements
        arr3[x] = arr1[i++];
    else if (arr1[i] <= arr2[j])
        arr3[x] = arr1[i++];
    else 
        arr3[x] = arr2[j++];

    x++;

For the second part of your question, simply check if the array is null or not.对于问题的第二部分,只需检查数组是否为空。

    if (arr1 == null) return arr2;
    else if (arr2 == null) return arr1;
    else
    {
         //Perform merge
    }

I hope I helped you.我希望我帮助了你。

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

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