Program:
#include<iostream>
#define size 1000
using namespace std;
int size1;
int* merge1(int a[],int const h1,int b[],int const h2)
{
static int c[size];
int h=0,r=0,i=0;
while(1)
{ if(h>=h1)
{while(r<h2)
c[i++]=b[r++];
break;
}
else if(r>=h2)
{
while(h<h1)
c[i++]=a[h++];
break;
}
else if(a[h]<b[r])
c[i++]=a[h++];
else
c[i++]=b[r++];
}
size1=h1+h2;
return c;
}
void disp(int a[],int len)
{
for(int i=0;i<len;i++)
cout<<a[i]<<" ";
cout<<endl;
}
int main()
{int n1,n2;
cout<<"Enter the size of array1 and array2:";
cin>>n1>>n2;
int *a=new int[n1];
int *b=new int[n2];
cout<<"Enter "<<n1<<" elements of array1:";
for(int i=0;i<n1;i++)
{
cin>>a[i];
}
cout<<"Enter "<<n2<<" elements of array2:";
for(int i=0;i<n2;i++)
{
cin>>b[i];
}
int *c=merge1(a,n1,b,n2);
cout<<"The merge_sort array is:";
disp(c,size1);
}
Output:
Enter the size of array1 and array2: 5 5
Enter 5 elements of array1: 1 3 4 6 8
Enter 5 elements of array2: 2 5 7 9 10
The merge_sort array is: 1 2 3 4 5 6 7 8 9 10
No comments:
Post a Comment
we are hear to discuss your queries ,so please feel free to ask any of your queries.