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
Define File organization''s and it''s types

The best average behaviour is shown by  Quick Sort

Q. Define a sparse matrix. Explain different types of sparse matrices? Show how a triangular array is stored in memory. Evaluate the method to calculate address of any element ajk

Determine the greatest common divisor (GCD) of two integers, m & n. The algorithm for GCD might be defined as follows: While m is greater than zero: If n is greater than m, s

Explain Backtracking The  principal idea is to construct solutions single component  at a time  and evaluate such  partially constructed candidates as follows. If a partiall

A Binary Search Tree is binary tree which is either empty or a node having a key value, left child & right child. By analyzing the above definition, we notice that BST comes int

A B-tree of minimum degree t can maximum pointers in a node T pointers in a node.

Chaining In this method, instead of hashing function value as location we use it as an index into an array of pointers. Every pointer access a chain that holds the element havi

Explain All-pair shortest-paths problem Given a weighted linked graph (undirected or directed), the all pairs shortest paths problem asks to find the distances (the lengths of

Ask question #explain it beriflyMinimum 100 words accepted#