#include<stdio.h>
#include<time.h>
#define MAX 50
void mergesort(int a[],int low,int high);
void merge(int a[],int low,int high,int low1,int high1);
double tc;
time_t start,end;
void main()
{
int a[MAX],n,i;
printf("Enter total no of elements:\n");
scanf("%d",&n);
printf("Enter the elements to be sorted:\n");
for(i=0;i<n;i++){
printf("enter the %d elements : \n",i+1);
scanf("%d",&a[i]);
printf("\n");
}
start=clock();
mergesort(a,0,n-1);
end=clock();
printf("\nAfter merge Sorted elements are :\n");
for(i=0;i<n;i++)
printf("%d ",a[i]);
tc=difftime(end,start)/CLOCKS_PER_SEC;
printf("time efficiency is %lf",tc);
}
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,mid+1,j);
}
}
void merge(int a[],int low,int high,int low1,int high1)
{
int temp[50];
int i,j,k;
i=low;
j=low1;
k=0;
while(i<=high && j<=high1)
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=high)
temp[k++]=a[i++];
while(j<=high1)
temp[k++]=a[j++];
for(i=low,j=0;i<=high1;i++,j++)
a[i]=temp[j];
}
Comments