#include<iostream>
#include<math.h>
using namespace std;
void merge(int a[],int low,int mid,int high);
void mergesort(int a[],int low,int high)
{
int mid;
if(low<high)
{
mid=((low+high)/2);
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);
}
}
int b[50];
void merge(int a[],int low,int mid,int high)
{
int i,j,k;
k=0;
i=low;
j=mid+1;
while(i<=mid && j<=high)
{
if(a[i]<a[j])
b[k++]=a[i++];
else
b[k++]=a[j++];
}
while(i<=mid)
{
b[k++]=a[i++];
}
while(j<=high)
b[k++]=a[j++];
for(i=high;i>=low;i--)
a[i]=b[--k];
}
int main()
{
int size,a[50],i;
cout<<"enter size of array:\t";
cin>>size;
cout<<"enter"<<size<<" element of array\n";
for(i=0;i<size;i++)
cin>>a[i];
mergesort(a,0,size-1);
cout<<"the sorted list is\n";
for(i=0;i<size;i++)
cout<<a[i]<<"\t";
return 0;
}
#include<math.h>
using namespace std;
void merge(int a[],int low,int mid,int high);
void mergesort(int a[],int low,int high)
{
int mid;
if(low<high)
{
mid=((low+high)/2);
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);
}
}
int b[50];
void merge(int a[],int low,int mid,int high)
{
int i,j,k;
k=0;
i=low;
j=mid+1;
while(i<=mid && j<=high)
{
if(a[i]<a[j])
b[k++]=a[i++];
else
b[k++]=a[j++];
}
while(i<=mid)
{
b[k++]=a[i++];
}
while(j<=high)
b[k++]=a[j++];
for(i=high;i>=low;i--)
a[i]=b[--k];
}
int main()
{
int size,a[50],i;
cout<<"enter size of array:\t";
cin>>size;
cout<<"enter"<<size<<" element of array\n";
for(i=0;i<size;i++)
cin>>a[i];
mergesort(a,0,size-1);
cout<<"the sorted list is\n";
for(i=0;i<size;i++)
cout<<a[i]<<"\t";
return 0;
}