将两个数组合并为一个

时间:2012-12-01 13:46:12

标签: java algorithm merge

    int [] queue1 = {4,7,2,9,12,35,8,49};
    int [] queue2 = {24,53,6,19,41,71,1,68,11,32,99}        
    int[]mergeQ = new int[queue1.length + queue2.length];

    for(int i=0; i < queue1.length; i++ )
    {

      mergeQ[i*2] = queue1[i];       
      mergeQ[i*2+1] = queue2[i];  
    }
    for(int i=0; i < mergeQ.length; i++) {            
        System.out.print(mergeQ[i]+",");
    }

输出:4,24,7,53,2,6,9,19,12,41,35,71,8,1,49,68,0,0,0

如何打印出queue2的其余元素。

6 个答案:

答案 0 :(得分:6)

您的合并算法假设queue1.length < queue2.length。虽然它对你的程序是正确的,但做出这样的假设通常不是一件好事。

更改合并算法以遍历两个数组,直到达到较短数组的长度,然后将较长数组的其余元素转储到合并数组的尾部。您可以在一个循环中完成所有操作,如下所示:

int p = 0;
for (int i = 0 ; i < queue1.length || i < queue2.length ; i++) {
    if (i < queue1.length) {
        mergeQ[p++] = queue1[i];
    }
    if (i < queue2.length) {
        mergeQ[p++] = queue2[i];
    }
}

答案 1 :(得分:1)

这是一种方法:

    int[] queue1 = { 4, 7, 2, 9, 12, 35, 8, 49 };
    int[] queue2 = { 24, 53, 6, 19, 41, 71, 1, 68, 11, 32, 99 };
    int[] mergeQ = new int[queue1.length + queue2.length];

    int dest = 0;
    int src1 = 0;
    int src2 = 0;
    while (src1 < queue1.length || src2 < queue2.length) {
        if (src1 < queue1.length) {
            mergeQ[dest++] = queue1[src1++];
        }
        if (src2 < queue2.length) {
            mergeQ[dest++] = queue2[src2++];
        }
    }

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

无论queue1还是queue2是否更短,这都有效。

它还保留算法的属性,即两个队列的元素是交错的。如果这不重要,那么整个事情可以被两次调用System.arraycopy()替换。

答案 2 :(得分:0)

我不确定是否有任何顺序需要合并两个队列(例如队列中的一个元素被队列2中的元素吞噬),尽管暂停代码会将所有元素放在合并队列中。

int [] 
    queue1 = {4,7,2,9,12,35,8,49},
    queue2 = {24,53,6,19,41,71,1,68,11,32,99},
    mergeQ = new int[queue1.length + queue2.length];

for (int i=0; i < queue1.length; i++ )
    mergeQ[i] = queue1[i];

for (int i = 0; i < queue2.length; i++)
    mergeQ[queue1.length + i] = queue2[i];

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

答案 3 :(得分:0)

int[] queue1 = {4,7,2,9,12,35,8,49};
int[] queue2 = {24,53,6,19,41,71,1,68,11,32,99}        
int[] mergeQ = new int[queue1.length + queue2.length

int i=0;
for(; i < queue1.length; i++ ){
  mergeQ[i*2] = queue1[i];       
  mergeQ[i*2+1] = queue2[i];
}
if(queue1.length>queue2.length){ 
 for(int j=i;j<queue1.length;j++)
  mergeQ[j+2] = queue1[j];
}else if(queue1.length<queue2.length){
 for(int j=i;j<queue2.length;j++)
  mergeQ[j+2] = queue2[j];
}

答案 4 :(得分:0)

检查哪个阵列更长。保留两者之间差异的计数器。稍微改变一下代码......多一点非正统......但更干净一点

int [] queue2 = {24,53,6,19,41,71,1,68,11,32,99};

int[]mergeQ = new int[queue1.length + queue2.length];

int larger=queue1.length;
int smaller=queue2.length;

if(queue1.length < queue2.length)
  {
   larger=queue2.length;
   smaller=queue1.length;
  }

    for(int i=0; i < queue1.length; i++ )
    {
      mergeQ[i*2] = queue1[i];       
      mergeQ[i*2+1] = queue2[i];

    }

    for(int i=mergeQ.length; i < queue2.length; i++ )
    {
       mergeQ[i] = queue2[larger-smaller];
       smaller++;
    }

    for(int i=0; i < mergeQ.length; i++){


        System.out.print(mergeQ[i]+",");
    }

这应该为你做

答案 5 :(得分:0)

输入:{1,3,5,7}和{2,4,6} 输出:{1,2,3,4,5,6,7}}

        int c[] = {2, 4, 6, 8};
        int n[] = {1, 3, 5, 7, 9};
        int f = 0;
        int res[] = new int[c.length + n.length];
        for(int i=0;i<(c.length + n.length); i++)
        {
            if(c.length > n.length)
            {
                if(i<c.length)
                    res[f++] = c[i];
                else if(f<=res.length-1)
                    res[f++] = c[i];

                if(i<n.length)
                res[f++] = (char) n[i];
            }else{
                if(i<n.length)
                    res[f++] = n[i];
                else if(f<=res.length-1)
                    res[f++] = n[i];

                if(i<c.length)
                res[f++] = (char) c[i];
            }

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