/*Author-Neelkant.S.Patil,GMIT,Davanagere*/
#include<stdio.h>
#include<conio.h>
#include<time.h>
void merge(int b[], int c[], int a[], int p, int q, int n)
{
int i,j,k;
i=j=k=0;
while(i<p && j<q)
{
if(b[i]<=c[j])
{
a[k]=b[i];
i++;
}
else
{
a[k]=c[j];
j++;
}
k++;
delay(100);
}
if(i==p)
{
while(j<q)
{
a[k++]=c[j++];
}
}
else
{
while(i<p && k<n)
a[k++]=b[i++];
}
}
void mergeSort(int a[],int n)
{
int b[10], c[10];
int i, j;
if(n>1)
{
for(i=0;i<n/2;i++)
b[i]=a[i];
for(i=n/2,j=0;i<n;i++,j++)
c[j]=a[i];
mergeSort(b, n/2);
mergeSort(c, n - n/2);
merge(b, c, a, n/2 ,n-n/2, n);
}
}
void main()
{
int a[10],i,j,n;
clock_t begin, end;
clrscr();
printf("Enter the value of n\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
begin=clock();
/**************** MERGE SORT IMPLEMNTATION ****************/
mergeSort(a,n);
/***************** <<<<<<<<<>>>>>>>>> ****************/
end=clock();
printf("The sorted array is\n");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
printf("\nBegin time=%d\n",begin);
printf("End time=%d\n",end);
printf("Total number of clock ticks=%d\n",end-begin);
printf("Total time required=%f\n",((end-begin)/CLK_TCK));
getch();
}
No comments:
Post a Comment