sorting
sorting
Selection 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 <stdlib.h>
#include <time.h>
int i, j, min_idx;
arr[min_idx] = arr[i];
arr[i] = temp;
{
for (int i = 0; i < n; i++)
int main()
int n;
if (n <= 5000)
if (arr == NULL)
generateRandomNumbers(arr, n);
free(arr);
return 0;
Outputs:
********************************************************************
********************************************************************
********************************************************************
********************************************************************
# data collected
time_taken = [0.031000, 0.034000, 0.047000, 0.052000, 0.077000] #replace with actual times recorded
plt.grid(True)
plt.show()
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 <stdlib.h>
#include <time.h>
int i, j, k;
i = 0;
j = 0;
k = left;
arr[k] = L[i];
i++;
else
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
arr[k] = R[j];
j++;
k++;
free(L);
free(R);
{
if (left < right)
int i;
int main()
int n,i;
scanf("%d", &n);
if (n <= 5000)
}
int *arr = (int *)malloc(n * sizeof(int));
if (arr == NULL)
generateRandomArray(arr, n);
// Repeat the sorting process multiple times to increase duration for timing
mergeSort(arr, 0, n - 1);
free(arr);
return 0;