Sie sind auf Seite 1von 3

Program to Sort an Element using Merge Sort

#include<iostream.h>
#include<conio.h>
#include<process.h>
template <class T>
class sorting
{ T a[50],b[50],c[50];
int mn,m,n;
public:
void getdata()
{
cout<<"\n\n\nIst ARRAY\n";
cout<<"---------";
cout<<"\n\n";
cout<<"\nHow many elements do u want to insert :";
cin>>m;
cout<<"\nEnter the array elements in Sorted order :";
for(int i=0;i<m;i++)
cin>>a[i];
cout<<"\n\n\nIInd ARRAY";
cout<<"---------";
cout<<"\n\n";

cout<<"\nHow many elements do u want to insert :";


cin>>n;
mn=m+n;
cout<<"\nEnter the array elements in Sorted order :";
for( i=0;i<n;i++)
cin>>b[i];
merge(a,m,b,n,c);
}
void display()
{
cout<<"\nMerged Array Elements in Sorted order is:";
for(int i=0;i<mn;i++)
{
cout<<c[i]<<"\t";
}
}
void merge(T a[],int m,T b[],int n,T c[])
{
int x,y,z;
for(x=0,y=0,z=0;x<m&&y<n;)
{
if(a[x]<=b[y])
c[z++]=a[x++];
else
c[z++]=b[y++];
}
if(x<m)
{
while(x<m)
{
c[z++]=a[x++];
}
}
else
{
while (y<n)
c[z++]=b[y++];
}
}
};
void main()
{
clrscr();
int choice;
char ch;
sorting <int>s1;
sorting <char>s2;
sorting <float>s3;
cout<<"\n\n\t\t\tMERGE SORT TECHNIQUE\n\n";
do
{

cout<<"\n\t\t1.INTEGER\n\t\t2.CHARACTER\n\t\t3.FLOAT\n\t\t4.EXIT";
cout<<"\n\t\tEnter your choice: ";
cin>>choice;
switch(choice)
{
case 1:
s1.getdata();
s1.display();
break;
case 2:
s2.getdata();
s2.display();
break;
case 3:
s3.getdata();
s3.display();
break;
case 4:
exit(1);

default:
cout<<"Wrong Choice!!";
}
cout<<"\nDo you Want to Continue..:";
cin>>ch;

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

getch();
}

Das könnte Ihnen auch gefallen