0% found this document useful (0 votes)
22 views4 pages

program For Merging Two Array in One Array in Sorted Order Using Merge Sort

This program merges two sorted arrays, A and B, into a single sorted array C. It contains functions to merge the arrays in ascending or descending order. The main function prompts the user to choose between these two merge options and displays the resulting merged array. It allows the user to repeatedly merge arrays until choosing to exit.

Uploaded by

minimol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

program For Merging Two Array in One Array in Sorted Order Using Merge Sort

This program merges two sorted arrays, A and B, into a single sorted array C. It contains functions to merge the arrays in ascending or descending order. The main function prompts the user to choose between these two merge options and displays the resulting merged array. It allows the user to repeatedly merge arrays until choosing to exit.

Uploaded by

minimol
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

//PROGRAM FOR MERGING TWO ARRAY IN ONE ARRAY IN SORTED

ORDER USING MERGE SORT

#include<iostream.h>

#include<process.h>

void mergeasc(int A[],int B[],int C[],int u1,int u2)

{ int ctrA=0,ctrB=0,ctrC=0;

while(ctrA<=u1 && ctrB<=u2)

if( A[ctrA] <= B[ctrB] )

{ C[ctrC]= A[ctrA]; ctrC=ctrC+1; ctrA=ctrA+1; }

else

{ C[ctrC]= B[ctrB]; ctrC=ctrC+1; ctrB=ctrB+1; }

if(ctrA>=u1)

{ while(ctrB<=u2)

{ C[ctrC]= B[ctrB]; ctrC=ctrC+1; ctrB=ctrB+1; }

if(ctrB>=u2)

{ while(ctrA<=u1)

{ C[ctrC]= A[ctrA]; ctrC=ctrC+1; ctrA=ctrA+1; }

} }
void mergedesc(int A[],int B[],int C[],int u1,int u2)

{ int ctrA=0,ctrB=u2-1,ctrC=(u1+u2)-1;

while(ctrA<=u1 && ctrB>=0)

{ if( A[ctrA] <= B[ctrB] )

{ C[ctrC]= A[ctrA]; ctrC=ctrC-1; ctrA=ctrA+1; }

else

{ C[ctrC]= B[ctrB]; ctrC=ctrC-1; ctrB=ctrB-1; }

} /* end of while */

if(ctrA>=u1)

{ while(ctrB>=0)

{ C[ctrC]= B[ctrB]; ctrC=ctrC-1; ctrB=ctrB-1; }

if(ctrB<0)

{ while(ctrA<=u1)

{ C[ctrC]= A[ctrA]; ctrC=ctrC-1; ctrA=ctrA+1; }

} }

void main()

int A[10],B[10],C[20],m,n,i,j,ch;

char opt='y';

do

{
cout<<"\n Menu";

cout<<"\n 1. A in ascending,B in Ascending ,C in Ascending";

cout<<"\n 2. A in ascending,B in descending,C in descending";

cout<<"\n 3. Exit";

cout<<"\n Enter ur choice::";

cin>>ch;

switch (ch)

{ case 1:{ cout<<"\n Enter the size array A::"; cin>>m;

cout<<"\n Enter the size array B::"; cin>>n;

cout<<"\n Enter the elements array A in ascending order::";

for(i=0;i<m;i++) cin>>A[i];

cout<<"\n Enter the elements array B in ascending order::";

for(i=0;i<n;i++) cin>>B[i];

mergeasc(A, B, C, m, n);

cout<<"\n Array after merging in ascending order is::\n";

for(i=0;i<m+n;i++) cout<<"\t"<<C[i];

break;

case 2:

{ cout<<"\n Enter the size array A::"; cin>>m;

cout<<"\n Enter the size array B::"; cin>>n;

cout<<"\n Enter the elements array A in ascending order::";

for(i=0;i<m;i++) cin>>A[i];
cout<<"\n Enter the elements array B in descending order::";

for(i=0;i<n;i++) cin>>B[i];

mergedesc(A,B,C,m,n);

cout<<"\n Array after merging in descending order is::\n";

for(i=0;i<m+n;i++) cout<<"\t"<<C[i];

break;

case 3: exit(0);

cout<<"\n Do u want to continue::";

cin>>opt;

}while(opt=='y' || opt=='Y');

You might also like