johnnily
johnnily

Reputation: 153

merge two array into one

    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]+",");
    }

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

how can i print out the rest of elements of queue2.?

Upvotes: 2

Views: 6900

Answers (6)

user3632799
user3632799

Reputation: 1

Input : {1, 3, 5, 7} and {2, 4, 6} Output : {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]);
        }
    }
}

Upvotes: 0

Dawson
Dawson

Reputation: 4403

I am not sure if there was any order in which you wanted the two queues merged (such as an element from queue one fallowed by an element from queue two) though the fallowing code will place all elements in the merge queue.

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]+",");

Upvotes: 0

IndianNoob
IndianNoob

Reputation: 167

Check which array is longer. Retain a counter for the difference between the two. Changing the code a bit...a little more unorthodox...but a little more clean

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]+",");
    }

This should do it for you

Upvotes: 0

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41210

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];
}

Upvotes: 0

NPE
NPE

Reputation: 500475

Here is one way to do it:

    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] + ",");
    }

This works regardless of whether queue1 or queue2 is shorter.

It also preserves the property of your algorithm that elements of the two queues are interleaved. If that's not important, then the whole thing can be replaced by two calls to System.arraycopy().

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726669

Your merge algorithm makes an assumption that queue1.length < queue2.length. Although it is correct for your program, it is not generally a good thing to make such assumptions.

Change the merge algorithm to go through both arrays until you hit the length of the shorter one, then dump the remaining elements of the longer array into the tail of the merged array. You can do it all in a single loop, like this:

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];
    }
}

Upvotes: 6

Related Questions