Pages

Wednesday, February 16, 2011

MERGING OF SORTED ARRAYS

#include<iostream.h>
#include<conio.h>
class merge
{
int i,j,k,m,n;
int a[10],b[10],c[20];
public:
void getar1();
void getar2();
void display();
};
void merge::getar1()
{
cout<<"\n"<<"enter size of 1st array (max 10)";
cin>>n;
cout<<"\n enter array";
for(i=1;i<=n;i++)
cin>>a[i];
}
void merge::getar2()
{
cout<<"\n enter size 2nd array (max 10)";
cin>>m;
cout<<"\n enter array";
for(j=1;j<=m;j++)
cin>>b[j];
}
void merge::display()
{
k=1; i=1;j=1;
cout<<"\n sorted array : \n";
while(i<=n&&j<=m)
{
if(a[i]<b[j])
{
c[k]=a[i];
k++;i++;
}
else
{
c[k]=b[j];
k++;j++;
}
}
if(i>n)
{
while(j<=m)
{
c[k]=b[j];
k++;j++;
}
}
else
{
while(i<=n)
{
c[k]=a[i];
k++;i++;
}
}
for(k=1;k<=m+n;k++)
cout<<c[k]<<"\t";
}
void main()
{
clrscr();
cout<<"\t///////////// MERGING///////////////\n\n\n";
merge obj;
obj.getar1();
obj.getar2();
obj.display();
getch();
}

No comments:

Post a Comment