#include
#include
void mergesort(int[],int,int);
void merge(int[],int,int,int);
void main()
{
clrscr();
int a[50],b[50],x,i;
cout<<"Enter the size of the array::";
cin>>x;
cout<
cin>>a[i];
mergesort(a,1,x);
cout<
cout< getch();
}
void mergesort(int a[],int low,int high)
{
int mid;
if(low
mid=floor((low+high)/2);
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);
}
}
void merge(int a[],int low,int mid,int high)
{
int b[50];
int h=low;
int i=low;
int j=mid+1;
while((h<=mid)&&(j<=high))
{
if(a[h]<=a[j])
{
b[i]=a[h];
h++;
}
else
{
b[i]=a[j];
j++;
}
i++;
}
if(h>mid)
{
for(int k=j;k<=high;k++)
{
b[i]=a[k];
i++;
}
}
else
{
for(int k=h;k<=mid;k++)
{
b[i]=a[k];
i++;
}
}
for(int k=low;k<=high;k++)
a[k]=b[k];
}
No comments:
Post a Comment