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

Experiment 11 Merge Sort

The document outlines a C/C++ program that implements the Merge Sort algorithm to sort a set of n integer elements and measures its time complexity. It includes functions for merging and sorting, and allows for input of random numbers or reading from a file. The program also records the time taken to sort and suggests plotting the time taken against varying values of n greater than 5000.

Uploaded by

apoorvanayak07
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)
4 views3 pages

Experiment 11 Merge Sort

The document outlines a C/C++ program that implements the Merge Sort algorithm to sort a set of n integer elements and measures its time complexity. It includes functions for merging and sorting, and allows for input of random numbers or reading from a file. The program also records the time taken to sort and suggests plotting the time taken against varying values of n greater than 5000.

Uploaded by

apoorvanayak07
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
You are on page 1/ 3

EXPERIMENT -11

/*Design and implement C/C++ Program to sort a given set of n integer elements using Merge Sort

method and compute its time complexity. Run the program for varied values of n> 5000, and

record the time taken to sort. Plot a graph of the time taken versus n. The elements can be read

from a file or can be generated using the random number generator.*/

#include <stdio.h>

#include <time.h>

#include <stdlib.h>

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

int i, j, k, b[100000];

i = low;

j = mid + 1;

k = low;

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

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

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

else

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

while (i <= mid)

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

while (j <= high)

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

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

a[k] = b[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);

void main() {

int n, k;

clock_t st, et;

double ts;

printf("\nEnter How many Numbers:");

scanf("%d", &n);

int a[100000]; // Declare array 'a' with size 'n'

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

for (k = 0; k < n; k++) { // Start from 0

a[k]=rand();

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

st = clock();

MergeSort(a, 0, n - 1);

et = clock();

ts = (double)(et - st) / CLOCKS_PER_SEC;

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

for (k = 0; k < n; k++) { // Start from 0

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

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

You might also like