Sel Merge Quick Sort
Sel Merge Quick Sort
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;
{
int i;
int main()
FILE *fp;
int n,i;
if (n <= 5000)
if (arr == NULL)
fp = fopen("f.dat", "w");
if (fp == NULL) {
generateRandomNumbers(arr, n);
selectionSort(arr, n);
for(i=0;i<n;i++)
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;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1],R[n2];
i = 0;
j = 0;
k = l;
arr[k] = L[i];
i++;
else
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
arr[k] = R[j];
j++;
k++;
if (l < r)
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
int i;
int main()
FILE *fp;
int arr[10000],n,i;
scanf("%d", &n);
if (n <= 5000)
fp = fopen("mf.dat", "w");
if (fp == NULL) {
exit(1);
generateRandomArray(arr, n);
// Repeat the sorting process multiple times to increase duration for timing
mergeSort(arr, 0, n - 1);
for(i=0;i<n;i++)
return 0;
Design and implement C/C++ Program to sort a given set of n integer elements using Quick 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 t = *a;
*a = *b;
*b = t;
int i, j, temp;
int p;
p = a[l];
i = l;
j = r + 1;
do {
do {
i++;
do {
j--;
swap(&a[i], &a[j]);
swap(&a[i], &a[j]);
swap(&a[l], &a[j]);
return j;
int s;
if (l < r) {
s = partition(a, l, r);
quickSort(a, l, s - 1);
quickSort(a, s + 1, r);
}
}
int i;
int main()
FILE *fp;
int i,n;
if (n <= 5000)
if (arr == NULL)
{
fp = fopen("qf.dat", "w");
if (fp == NULL) {
exit(1);
generateRandomNumbers(arr, n);
for(i=0;i<10;i++)
quickSort(arr, 0, n - 1);
for(i=0;i<n;i++)
free(arr);
return 0;