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

Lab11-Merge Sort

The document outlines a C/C++ program that implements the Merge Sort algorithm to sort a set of integer elements and measures its time complexity for varying sizes greater than 5000. It includes the algorithm, program code, and instructions for generating random integers and recording sorting times. The results show the time taken to sort arrays of different sizes, demonstrating the efficiency of the Merge Sort method.

Uploaded by

ananya.r.amcec
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)
19 views3 pages

Lab11-Merge Sort

The document outlines a C/C++ program that implements the Merge Sort algorithm to sort a set of integer elements and measures its time complexity for varying sizes greater than 5000. It includes the algorithm, program code, and instructions for generating random integers and recording sorting times. The results show the time taken to sort arrays of different sizes, demonstrating the efficiency of the Merge Sort method.

Uploaded by

ananya.r.amcec
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
You are on page 1/ 3

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. .
Aim:
To sort a given set of n integer elements using Merge sort and plot the graph between time and different number of Values
(n > 5000).
Algorithm:
ALGORITHM Merge(B[0..p - 1],C[0..q - 1],A[0..p + q - 1])
//Merges two sorted arrays into one sorted array
//Input: Arrays B[0..p - 1] and C[0..q - 1] both sorted
//Output: Sorted array A[0..p + q - 1] of the elements of B and C
i ←0; j ←0; k ←0
while i<pand j<qdo
if B[i] = C[j ]
A[k] ←B[i]; i ←i + 1
else
A[k] ←C[j ]; j ←j + 1
k ←k + 1
if i = p
copy C[j..q - 1] to A[k..p + q - 1]
else copy B[i..p - 1] to A[k..p + q - 1]
Program:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Function to merge two sorted arrays
void merge(int arr[], int left, int mid, int right)
{
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;
int *L = (int *)malloc(n1 * sizeof(int));
int *R = (int *)malloc(n2 * sizeof(int));
for (i = 0; i < n1; i++)
L[i] = arr[left + i];
for (j = 0; j < n2; j++)
R[j] = arr[mid + 1 + j];
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
free(L);
free(R);
}
// Function to implement Merge Sort
void mergeSort(int arr[], int left, int right)
{
if (left < right)
{
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
// Function to generate random integers
void generateRandomArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
arr[i] = rand() % 100000; // Generate random integers between 0 and 99999
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
if (n <= 5000)
{
printf("Please enter a value greater than 5000\n");
return 1; // Exit if the number of elements is not greater than 5000
}
int *arr = (int *)malloc(n * sizeof(int));
if (arr == NULL)
{
printf("Memory allocation failed\n");
return 1; // Exit if memory allocation fails
}
generateRandomArray(arr, n);
// Repeat the sorting process multiple times to increase duration for timing
clock_t start = clock();
for (int i = 0; i < 1000; i++)
{
mergeSort(arr, 0, n - 1);
}
clock_t end = clock();
// Calculate the time taken for one iteration
double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC / 1000.0;
printf("Time taken to sort %d elements: %f seconds\n", n, time_taken);
free(arr);
return 0;
}
Input:
Enter number of elements: 10000
Enter number of elements: 20000
Enter number of elements: 30000
Enter number of elements: 40000
Enter number of elements: 50000

Output:
Time taken to sort 10000 elements: 0.001025 seconds
Time taken to sort 20000 elements: 0.002359 seconds
Time taken to sort 30000 elements: 0.003895 seconds
Time taken to sort 40000 elements: 0.005211 seconds
Time taken to sort 50000 elements: 0.005807 seconds

Conclusion:
Thus the program was executed sucessfully

You might also like