Java 合并长度不等的排序数组

Java 合并长度不等的排序数组,java,merge,mergesort,Java,Merge,Mergesort,我有一个项目,需要我合并两个排序的数组(a和b),并将结果放入一个长度为a.length+b.length的新数组中。 我在跟踪所有3个数组中放置的计数器,数组的长度不相等。我的惯例是,如果一个数组在另一个数组之前用完,代码将把另一个数组的其余部分转储到结果数组中 不幸的是,检查另一个数组是否仍然包含元素的唯一方法是for循环 有人能帮我吗?这应该是一个相对容易的解决方案,但我想不出解决方案 public class Two { public static void main(Strin

我有一个项目,需要我合并两个排序的数组(a和b),并将结果放入一个长度为a.length+b.length的新数组中。 我在跟踪所有3个数组中放置的计数器,数组的长度不相等。我的惯例是,如果一个数组在另一个数组之前用完,代码将把另一个数组的其余部分转储到结果数组中

不幸的是,检查另一个数组是否仍然包含元素的唯一方法是for循环

有人能帮我吗?这应该是一个相对容易的解决方案,但我想不出解决方案

public class Two {
    public static void main(String[] args) {
        //sample problem
        int[] var_a = {2,3,5,5,8,10,11,17,18,20}; 
        int[] var_b = {5,6,7,8,14,15,17};
        final int a_size = 10;
        final int b_size = 7;
        final int c_size = 17; 
        int[] var_c = new int[17];

        int aCount = 0;
        int bCount = 0;
        int cCount = 0;
        for (cCount = 0; cCount < c_size; cCount++) {
            //b runs out before a runs out
            if ((bCount == b_size) && (aCount <= a_size)) {
                //dump rest of var_a into var_c     
                var_c[cCount] = var_a[aCount];
                aCount++;
            }
            //PROBLEM: bCount is equal to bSize, and is triggering the break.
            //a runs out before b runs out
            else if ((aCount == a_size) && (bCount <= b_size)) {
                //dump rest of var_b into var_c
                var_c[cCount] = var_b[bCount];
                bCount++;
            }

            if ((aCount >= a_size) || (bCount >= b_size) || (cCount >= c_size)) {break;}

            if (var_a[aCount] < var_b[bCount]) {
                var_c[cCount] = var_a[aCount];
                aCount++;
            } else if (var_a[aCount] > var_b[bCount]) {
                var_c[cCount] = var_b[bCount];
                bCount++;
            } else if (var_a[aCount] == var_b[bCount]) {
                var_c[cCount] = var_a[aCount];
                aCount++;
                cCount++;
                var_c[cCount] = var_b[bCount];
                bCount++;
            }
        }
        for (int i : var_c) {
            System.out.print(i + " ");
        }
    }
}
公共二级{
公共静态void main(字符串[]args){
//样本问题
int[]var_a={2,3,5,5,8,10,11,17,18,20};
int[]var_b={5,6,7,8,14,15,17};
最终int a_尺寸=10;
最终int b_尺寸=7;
最终int c_尺寸=17;
int[]var_c=新int[17];
int aCount=0;
int bCount=0;
int cCount=0;
对于(cCount=0;cCount=c_大小)){break;}
如果(var_a[a账户]变量b[b计数]){
var_c[Account]=var_b[bCount];
bCount++;
}否则如果(变量a[a计数]==变量b[b计数]){
var_c[Account]=var_a[Account];
aCount++;
cCount++;
var_c[Account]=var_b[bCount];
bCount++;
}
}
用于(int i:var_c){
系统输出打印(i+“”);
}
}
}

您可以迭代单个数组的索引,而不是迭代合并数组的索引,以确保永远不会越界:

int aCount = 0;
int bCount = 0;
int cCount = 0;
while (aCount < var_a.length && bCount < var_b.length) {
    if (var_a[aCount] <= var_b[bCount]) {
        var_c[cCount++] = var_a[aCount++];
    } else {
        var_c[cCount++] = var_b[bCount++];
    }
}
// now add what remains of var_a or var_b
while (aCount < var_a.length)
    var_c[cCount++] = var_a[aCount++];
while (bCount < var_b.length)
    var_c[cCount++] = var_b[bCount++];
int aCount=0;
int bCount=0;
int cCount=0;
while(帐户如果(var_a[aCount]此问题的常见解决方案是将单个循环替换为三个:

  • 第一个循环合并两个数组,直到其中一个数组的元素用完为止
  • 第二个循环将数组
    A
    的剩余元素(如果有)转储到输出数组中
  • 第三个循环将数组
    B
    的剩余元素(如果有)转储到输出数组中
此结构使合并循环更简单:

while (aCount != var_a.length() && b.Count != var_b.length()) {
    ... // merge
}
while (aCount != var_a.length()) {
    var_c[cCount++] = var_a[aCount++];
}
while (bCount != var_b.length()) {
    var_c[cCount++] = var_b[bCount++];
}

请注意,在最后两个循环中,最多将执行一个循环。还要注意使用
length()
方法来确定数组的长度。这比设置
a_size
b_size
等更可靠。

此代码应遵循

  • 将所有3个a_计数器、b_计数器、c_计数器的计数器初始化为0
  • 现在,当a_c或b_计数器分别小于a_大小和b_大小时
    • 将a[a_计数器]与b[b_计数器]进行比较,并在c[c_计数器]处将较小的值与c相加
    • 增量c_计数器,上面选择了哪个
    • 重复
  • 一旦出环,a或b中的一个结束
    • 如果b结束,则继续将a的所有剩余元素添加到c;如果a结束,则继续将b添加到c 我相信您不需要代码,只需要改进逻辑;因此不需要提供代码

  • 这可能是您想要的:

    void doArrayThing(){
        //the two arrays containing the elements
        int[] array1 = {1, 3, 5, 2, 7, 5};
        int[] array2 = {3, 6, 2, 8};
    
        //the length of the 2 arrays
        int arrayLength1 = array1.length;
        int arrayLength2 = array2.length;
    
        //the length of both the arrays
        int containerArrayLength = arrayLength1 + arrayLength2;
    
        //the array that holds all the elements
        int[] container = new int[containerArrayLength];
    
        //a for loop that adds the first array elements
        for(int i = 0; i < arrayLength1; i++){
            //add the elements of the first array
            container[i] = array1[i];
        }
    
        //the for loop that adds the second array elements
        for(int i = 0; i < arrayLength2; i++){
            //add the second array elements on top of the first
            container[i + arrayLength1] = array2[i];
        }
    
        for(int i = 0; i < containerArrayLength; i++){
            //print all the elements
            System.out.println(container[i]);
        }
    }
    
    void doArrayThing(){
    //包含元素的两个数组
    int[]数组1={1,3,5,2,7,5};
    int[]array2={3,6,2,8};
    //2个数组的长度
    int ARRAYLENGT1=array1.length;
    int ARRAYLENGT2=array2.length;
    //两个数组的长度
    int containerArrayLength=阵列长度1+阵列长度2;
    //包含所有元素的数组
    int[]容器=新的int[containerArrayleLength];
    //添加第一个数组元素的for循环
    对于(int i=0;i
    它的作用是:


    它在第一个数组上迭代并添加数字,然后在第二个数组上迭代并在第一个数组元素上添加元素。

    您的算法看起来基本正确。如果不运行确切的代码,我认为您的问题主要是等式太多,即几乎所有比较都是eq不需要的,比同等的多

    如果a==b,那么a=b。你的If语句中有太多的语句因此而匹配。请密切关注具体的索引应该是什么,并将你的情况限制在它们确切应该是什么

    我已经重写了你的代码(对不起,没有编辑器,所以可能无法开箱即用),这应该会给你一个很好的想法

    public class Two 
    {
        public static void main(String[] args) 
        {
            //sample problem
            int[] arrayA = {2,3,5,5,8,10,11,17,18,20}; 
            int[] arrayB = {5,6,7,8,14,15,17};
            final int sizeA = arrayA.length();
            final int sizeB = arrayB.length();
            final int sizeC = sizeA+sizeB; 
            int[] arrayC = new int[sizeC];
            int countA = 0;
            int countB = 0;
            int countC = 0;
            for (countC = 0; countC < sizeC; countC++)
            {
                // if a has run out, fill with b
                if (countA == sizeA && countB < sizeB)
                {
                    arrayC[countC] = arrayB[countB];
                    countB++;
                }
                // if b has run out, fill with a
                else if (countA < sizeA && countB == sizeB)
                {
                    arrayC[countC] = arrayA[countA];
                    countA++;
                }
                // countA < sizeA && countB < sizeB because 
                // if countA == sizeA && countB == sizeB then also countC == sizeC 
                // and the for-loop would have stopped.
                else        
                {
                    // mind, if arrayA[countA] == arrayB[countB] then first 
                    // a will be added and on the next pass b will be added
                    if (arrayA[countA] <= arrayB[countB])
                    {
                        arrayC[countC] = arrayA[countA];
                        countA++;
                    }
                    else
                    {
                        arrayC[countC] = arrayB[countB];
                        countB++;
                    }
                }
            }
        }
    }
    
    公共二级
    {
    公共静态void main(字符串[]args)
    {
    //样本问题
    int[]arrayA={2,3,5,5,8,10,11,17,18,20};
    int[]arrayB={5,6,7,8,14,15,17};
    final int sizeA=arrayA.length();
    final int sizeB=arrayB.length();
    最终整数sizeC=sizeA+sizeB;
    int[]arrayC=新的int[sizeC];
    int countA=0;
    int countB=0;
    int countC=0;
    对于(countC=0;countC