//Mergesort
#include<stdio.h>
void merge(int a[], int low ,int high);
void mergesort(int a[], int low ,int mid ,int high);
int comp_cnt,swap_cnt;
void main()
{
int i,a[20],n;
printf("how many elements you want to enter:-");
scanf("%d",&n);
printf("enter %d elements: ",n);
for(i=0; i<n; i++)
scanf("%d",&a[i]);
merge(a,0,n-1);
printf("shorted elements is: \t \n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
void mergesort(int a[], int low ,int mid ,int high)
{
int i=low,j=mid+1,k=0,b[20];
comp_cnt++;
while((i<=mid)&&(j<=high))
{
comp_cnt++;
if(a[i]<a[j])
{
swap_cnt++;
b[k++]=a[i++];
}
else
{
swap_cnt++;
b[k++]=a[j++];
}
}
comp_cnt++;
while(i<=mid)
{
swap_cnt++;
b[k++]=a[i++];
}
comp_cnt++;
while(j<=high)
{
swap_cnt++;
b[k++]=a[j++];
}
for(j=low, k=0; j<=high; j++,k++)
{
swap_cnt++;
a[j]=b[k];
}
}
void merge(int a[],int low ,int high)
{
int mid;
comp_cnt++;
if(low<high)
{
mid=(low+high)/2;
merge(a,low,mid);
merge(a,mid+1,high);
mergesort(a,low,mid,high);
}
}