简体   繁体   English

将两个排序数组排序为新排序数组的最有效方法

[英]most efficient way to sort two sorted array into new sorted array

I am trying to sort two int arrays into a new int array in ascending order.我正在尝试按升序将两个 int 数组排序为一个新的 int 数组。

I wrote it like this and it sorted everything but the last element in textArr and I couldn't figure out why...我是这样写的,它对除 textArr 中最后一个元素之外的所有内容进行排序,但我不明白为什么......

int i=0, j=0, k=0;
while(i < textArr.length && j < binaryArr.length) {
    if(textArr[i] < binaryArr[j]) {
        mergeArr[k] = textArr[i];
        i++;
    }
    else {
        mergeArr[k] = binaryArr[j];
        j++;
    }
    k++;
}

I looked around and found a solution that added two new while loops but I honestly do not know what those second two while loops are doing.我环顾四周,找到了一个添加两个新 while 循环的解决方案,但老实说,我不知道后两个 while 循环在做什么。 I'd also like to know if this is the most efficient way to sort two arrays into a third sorted array?我还想知道这是否是将两个数组排序为第三个排序数组的最有效方法? Thanks for any help, I'm very new to java and would like to learn correctly and obviously I want to learn what I'm actually writing too!感谢您的帮助,我对 Java 很陌生,想正确学习,显然我也想学习我实际写的东西!

int i=0, j=0, k=0;
while(i < textArr.length && j < binaryArr.length) {
    if(textArr[i] < binaryArr[j]) {
        mergeArr[k] = textArr[i];
        i++;
    }
    else {
        mergeArr[k] = binaryArr[j];
        j++;
    }
    k++;
}
while(i < textArr.length) {
    mergeArr[k] = textArr[i];
    i++;
    k++;
}       

Here is your code with comments explaining why the final two loops are required.这是带有注释的代码,解释了为什么需要最后两个循环。

int[] textArr = {1, 2, 3, 3, 9};
int[] binaryArr = {4, 5, 6};
int[] mergeArr = new int[textArr.length + binaryArr.length];

int i=0, j=0, k=0;
while(i < textArr.length && j < binaryArr.length) {
    if(textArr[i] < binaryArr[j]) {
        mergeArr[k] = textArr[i];
        i++;
    }
    else {
        mergeArr[k] = binaryArr[j];
        j++;
    }
    k++;
}
// At this point either i == textArr.length or j == binaryArr.length
// so you cannot do textArr[i] < binaryArr[j] without getting
// an ArrayIndexOutOfBoundsException. 
// However the algorithm isn't finished. You still need to transfer
// the remaining numbers from one of the original arrays into the answer.
while(i < textArr.length) {
    mergeArr[k] = textArr[i];
    i++;
    k++;
}
while(j < binaryArr.length) {   // It said i in question. I corrected.
    mergeArr[k] = binaryArr[j];
    k++;
    j++;
}

System.out.println(Arrays.toString(mergeArr));

As for whether this is the most efficient code.至于这是否是最高效的代码。 It's about as good as you can get.这是你能得到的最好的。 Any algorithm has to read all the numbers so you can't do better than O(n + m) where n and m are the lengths of the original arrays.任何算法都必须读取所有数字,因此您不能做得比O(n + m)更好,其中nm是原始数组的长度。 You could slightly improve performance by replacing the final while loops with System.arraycopy .您可以通过用System.arraycopy替换最后的while循环来稍微提高性能。

Basic algorithm for merging two sorted arrays of size n .合并两个大小为n排序数组的基本算法。 This should be enough to go off of for the more generic "merge two sorted arrays of size m and n respectively"对于更通用的“分别合并两个大小为mn排序数组”,这应该足够了

//given arrays A, B, create C
var i, j = 0, k = 0;
for (i = 0; i < 2n; i++) {
  if (A[j] < B[k]) { 
    C[i] = A[j++]; 
  }
  else { 
    C[i] = B[k++];
  }
}

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

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