0% found this document useful (0 votes)
3 views4 pages

Merge Sort Algorithm with Timing Analysis

The document describes a program that sorts a set of elements using the merge sort algorithm and measures the time required for different set sizes. The program prompts the user to enter the number of elements, reads the elements, sorts the array using merge sort, displays the sorted array, and prints the time taken by the sorting algorithm. The time taken is measured by calling the clock() function before and after the sorting and dividing the difference by CLOCKS_PER_SEC.

Uploaded by

tejaswini n
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Merge Sort Algorithm with Timing Analysis

The document describes a program that sorts a set of elements using the merge sort algorithm and measures the time required for different set sizes. The program prompts the user to enter the number of elements, reads the elements, sorts the array using merge sort, displays the sorted array, and prints the time taken by the sorting algorithm. The time taken is measured by calling the clock() function before and after the sorting and dividing the difference by CLOCKS_PER_SEC.

Uploaded by

tejaswini n
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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: -

You might also like