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
Which sorting methods would be most suitable for sorting a list which is almost sorted  Bubble Sorting method.

Q. Describe the basic concept of binary search technique? Is it more efficient than the sequential search?         Ans : The bin ary search technique:- This tec

What values are automatically assigned to those array elements which are not explicitly initialized? Garbage values are automatically assigned to those array elements that

A driver takes shortest possible route to attain destination. The problem which we will discuss here is similar to this type of finding shortest route in any specific graph. The gr

Hubs - In reality a multiport repeater - Connects stations in a physical star topology - As well may create multiple levels of hierarchy to remove length limitation of 10

How can we convert a graph into a tree ? Do we have any standardized algorithm for doing this?

how to draw a 5 inch square on the screen using * symbol

Consider the file " search_2013 ". This is a text file containingsearch key values; each entry is a particular ID (in the schema given above). You are tosimulate searching over a h

How to create an General Tree and how to search general tree?

include include include /* Definition of structure node */ typedef struct node { int data; struct node *next; } ; /* Definition of push function */