0% found this document useful (0 votes)
16 views3 pages

Merge Sort

The document provides a C program implementing the merge sort algorithm to sort 'n' unordered elements. It measures the execution time for sorting and includes functionality to input the number of elements and display the sorted results. Additionally, it suggests plotting a graph of time taken versus different values of 'n' to analyze performance.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views3 pages

Merge Sort

The document provides a C program implementing the merge sort algorithm to sort 'n' unordered elements. It measures the execution time for sorting and includes functionality to input the number of elements and display the sorted results. Additionally, it suggests plotting a graph of time taken versus different values of 'n' to analyze performance.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

3. Implement merge sort algorithm to sort the given ‘n’ unordered elements .

Determine the time


to sort the elements for different values of ‘n’ and plot a graph of the time taken versus ‘n’.

#include <stdio.h>

#include<time.h>

#include<stdlib.h>

#include<windows.h>

int C[20];

void Merge(int a[ ], int low, int mid, int high)

int i, j, k; i=low; j=mid+1; k=low;

while ( i<=mid && j<=high )

if( a[i] <= a[j] )

C[k++] = a[i++] ;

else

C[k++] = a[j++] ;

while (i<=mid)

C[k++] = a[i++] ;

while (j<=high)

C[k++] = a[j++] ;

for(k=low; k<=high; k++)

a[k] = C[k];

void MergeSort(int a[], int low, int high)


{

int mid;

if(low >= high)

return;

mid = (low+high)/2 ;

MergeSort(a, low, mid);

MergeSort(a, mid+1, high);

Merge(a, low, mid, high);

int main( )

int n, a[100],k;

clock_t st,et;

double ts;

printf("\n Enter the number of elements to be sorted:");

scanf("%d", &n);

printf("\nThe Numbers are:\n");

for(k=1; k<=n; k++)

scanf("%d",&a[k]);

LARGE_INTEGER frequency,startTime,endTime;

QueryPerformanceFrequency(&frequency);

QueryPerformanceCounter(&startTime);

MergeSort(a, 1, n);
QueryPerformanceCounter(&endTime);

printf("\n\n\n Sorted Numbers are : \n ");

for(k=1; k<=n; k++)

{printf("%d\t", a[k]);

printf("\nThe time taken is %e",ts);

double executionTime=(double)([Link])/[Link];

printf("\n Execution time:%.9f seconds\n",executionTime);

return 0;

You might also like