Algorithm to merge two sorted arrays with third array, Data Structure & Algorithms

Q. Write down an algorithm to merge the two sorted arrays into the third array. Do  not perform the sort function in the third array.                          

Ans:

void merge(int *a,int *b,int n,int m)

{

int i=0,c[20],j=0,k=0,count=0;

while(i<=n&&j<=m)

{

if(a[i]

{

c[k]=a[i];

i++;

k++;

}

if(a[i]>b[j])

{

c[k]=b[j];

j++;

k++;

}

if(a[i]==b[j])

{

c[k]=a[i];

k++; i++; j++;

count++;

}

}

if(i<=n&&j==m)

{

while(i<=n)

{

c[k]=a[i];

i++;

k++;

}

}

if(i==n&&j<=m)

{

while(j<=m)

{

c[k]=b[j];

i++;

j++;

}

}

for(i=0;i

printf("%d\t",c[i]);

}

Posted Date: 7/10/2012 7:07:26 AM | Location : United States







Related Discussions:- Algorithm to merge two sorted arrays with third array, Assignment Help, Ask Question on Algorithm to merge two sorted arrays with third array, Get Answer, Expert's Help, Algorithm to merge two sorted arrays with third array Discussions

Write discussion on Algorithm to merge two sorted arrays with third array
Your posts are moderated
Related Questions
HEAP  A heap is described to be a binary tree with a key in every node, such that  1-All the leaves of the tree are on 2 adjacent levels. 2- All leaves on the lowest leve

Q. Write  down a   programme  in  C  to  create  a  single  linked  list also  write the functions to do the following operations (i)  To insert a new node at the end (ii

Define Wire-frame Model This skeletal view is called a Wire-frame Model. Although not a realistic representation  of the object, it is still very useful in the early stages of

Define about the class invariant A class invariant may not be true during execution of a public operation though it should be true between executions of public operations. For

A striking application of DFS is determine a strongly connected component of a graph. Definition: For graph G = (V, E) , where V refer to the set of vertices and E refer to the

Postorder traversal of a binary tree struct NODE { struct NODE *left; int value;     /* can take any data type */ struct NODE *right; }; postorder(struct NODE

Memory Allocation Strategies If it is not desirable to move blocks of due storage from one area of memory to another, it must be possible to relocate memory blocks that have be

what is queues? how it work? and why it used? i want an assignment on queue .....

Comp are two functions n 2    and  2 n  / 4  for distinct values of n.   Determine When s ec on d function b ec om es l a r g er th an f i r st functi

Give example of assertion and abstract data type For illustration, consider Natural ADT whose carrier set is the set of non-negative integers and whose operations are the usual