Sort a given set of elements using Merge Sort method and
determine the time of required to sort the elements.
Repeat the experiment for different of values of n.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
void main ()
{
int i, low, high;
int a [10], t [10], n;
clock_t s, e, ts;
void merge sort (int a [10], int low, int high);
void display (int a [10], int n);
clrscr ();
printf ("enter the number of elements");
scanf ("%d", &n);
printf ("enter the elements\n");
for (i=0; i<n; i++)
scanf ("%d", &a[i]);
low=0;
high=n-1;
s=clock ();
merge sort (a, low, high);
e=clock ();
display (a, n);
ts=((double)(e-s))/CLOCKS_PER_SEC;
printf ("\nThe time required is %e", ts);
getch ();
}
void merge sort (int a [], int low, int high)
{
int mid;
void combine (int a [], int low, int mid, int high);
if(low<high)
{
mid= (low +high)/2;
merge sort (a, low, mid);
merge sort (a, mid+1, high);
combine (a, low, mid, high);
}
}
void combine (int a [], int low, int mid, int high)
{
int i, j, k;
int t [10];
k=low;
i=low;
j=mid+1;
while (i<=mid && j<=high)
{
if(a[i]<=a[j])
{
t[k]=a[i];
i=i+1;
k=k+1;
}
else
{
t[k]=a[j];
k=k+1;
j=j+1;
}
}
while(i<=mid)
{
t[k]=a[i];
i=i+1;
k=k+1;
}
while(j<=high)
{
t[k]=a[j];
j=j+1;
k=k+1;
}
for (k=low; k<=high; k++)
a[k]=t[k];
}
void display (int a [10], int n)
{
int i;
printf ("the sorted array is");
for (i=0; i<n; i++)
printf ("\t%d”, a[i]);
}
Output: -