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

compare two functions n and 2n for various values of n. determine when second becomes larger than first

sdsd.

What is complexity of an algorithm? What is the basic relation between the time and space complexities of an algorithm? Justify your answer by giving an example.

Graph Traversal In many problems we wish to investigate all the vertices in a graph in some systematic order. In graph we often do not have any single vertex singled out as spe

Explain about the String Abstract data type operations Symbol ADT has no concatenation operations, but presuming we have a full-featured String ADT, symbols can be concatenated

In order to analyze an algorithm is to find out the amount of resources (like time & storage) that are utilized to execute. Mostly algorithms are designed to work along with inputs

Let G=(V,E) be a graph for which all nodes have degree 5 and where G is 5-edge is connected. a) Show that the vector x which is indexed by the edges E and for which xe = 1/5 for

The two famous methods for traversing are:- a) Depth first traversal b) Breadth first

A queue is a, FIFO (First In First Out) list.