#include<>
class msort
{
int n,a[20];
public:
void msplit(int a[20],int first,int last);
void merge(int a[20],int f1,int l1,int f2,int l2);
};
void msort::msplit(int a[20],int first,int last)
{
int mid;
if(first
mid=(first+last)/2;
msplit(a,first,mid);
msplit(a,mid+1,last);
merge(a,first,mid,mid+1,last);
}
}
void msort::merge(int a[20],int f1,int l1,int f2,int l2)
{
int i,j,k=0,temp[20];
i=f1;
j=f2;
while((i<=l1)&&(j<=l2))
{
if(a[i] {
temp[k]=a[i];
i++;
}
else
{
temp[k]=a[j];
j++;
}
k++;
}
while(i<=l1)
{
temp[k]=a[i];
i++;
k++;
}
while(j<=l2)
{
temp[k]=a[j];
j++;
k++;
}
i=f1;
j=0;
while((i< =l2)&&(j< =k))
{
a[i]=temp[j];;
i++;
j++;
}
}
{
int i,a[20],n;
clrscr();
cout<<"\n\n\t\tMERGE SORT\n";
cout<<"\t\t---------------------\n";
msort x;
cout<<"\nEnter the n value:";
cin>>n;
cout<<"\nEnter the array values:\n";
for(i=0;i
x.msplit(a,0,n-1);
cout<<"\nThe Sorted list is:\n";
for(i=0;i
}
OUTPUT WILL BE
Enter the n value: 5
Enter the array values:
5 9 2 7 1
The Sorted list is:
1 2 5 7 9
No comments:
Post a Comment