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

Selection Sort_csv With Graph

The document contains a C program that implements the Selection Sort algorithm to sort a set of integer elements and measures its time complexity for varying sizes of input (n > 5000). It generates random integers, sorts them, and records the time taken in a CSV file. Additionally, there is Python code provided to plot a graph of the time taken versus the number of elements sorted.

Uploaded by

madhukeshs0742
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)
4 views5 pages

Selection Sort_csv With Graph

The document contains a C program that implements the Selection Sort algorithm to sort a set of integer elements and measures its time complexity for varying sizes of input (n > 5000). It generates random integers, sorts them, and records the time taken in a CSV file. Additionally, there is Python code provided to plot a graph of the time taken versus the number of elements sorted.

Uploaded by

madhukeshs0742
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/ 5

C:\Users\jayap\Desktop\2025-2025-Even Semester\BCS401-Analysis and Design of Algorithms\404 Lab Programs\9 Prg\SelectionSort_CSV.

c Friday, 18 April, 2025 01:09 PM

/* 9. Design and implement C/C++ Program to sort a given set of


n integer elements using 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>

#define MAX_SIZE 100000

// Selection Sort function


void selectionSort(int arr[], int n)
{
int i, j, minIdx, temp;

for (i = 0; i < n - 1; i++)


{
minIdx = i;

for (j = i + 1; j < n; j++)


{
if (arr[j] < arr[minIdx])
minIdx = j;
-1-
C:\Users\jayap\Desktop\2025-2025-Even Semester\BCS401-Analysis and Design of Algorithms\404 Lab Programs\9 Prg\SelectionSort_CSV.c Friday, 18 April, 2025 01:09 PM

// Swap
temp = arr[minIdx];
arr[minIdx] = arr[i];
arr[i] = temp;
}
}

int main()
{
int arr[MAX_SIZE];
int n;

FILE *fp = fopen("time_complexity_data.csv", "w");


if (fp == NULL)
{
printf("Error opening file.\n");
return 1;
}

fprintf(fp, "n,time(ms)\n");

printf("Running Selection Sort for values of n > 5000...\n");

// Test for values of n: 5000 to 50000 in steps of 5000


for (n = 5000; n <= 50000; n += 5000)
-2-
C:\Users\jayap\Desktop\2025-2025-Even Semester\BCS401-Analysis and Design of Algorithms\404 Lab Programs\9 Prg\SelectionSort_CSV.c Friday, 18 April, 2025 01:09 PM

{
// Generate random array
for (int i = 0; i < n; i++)
{
arr[i] = rand() % 100000;
}

clock_t start = clock();

selectionSort(arr, n);

clock_t end = clock();

double time_taken = ((double)(end - start)) * 1000.0 /


CLOCKS_PER_SEC;

printf("n = %d\tTime = %.3f ms\n", n, time_taken);


fprintf(fp, "%d,%.3f\n", n, time_taken);
}

fclose(fp);

printf("\nTime results saved to 'time_complexity_data.csv'.\n");


return 0;
}

-3-
C:\Users\jayap\Desktop\2025-2025-Even Semester\BCS401-Analysis and Design of Algorithms\404 Lab Programs\9 Prg\SelectionSort_CSV.c Friday, 18 April, 2025 01:09 PM

/* OUTPUT
time_complexity_data.csv

n time(ms)
5000 25
10000 97
15000 222
20000 385
25000 623
30000 910
35000 1196
40000 1572
45000 1912
50000 2389

*/

# Python Code to Plot Graph

import pandas as pd
import matplotlib.pyplot as plt

data = pd.read_csv('C:/Users/jayap/Desktop/2025-2025-Even
Semester/BCS401-Analysis and Design of Algorithms/404 Lab Programs/9
Prg/time_complexity_data.csv')
-4-
C:\Users\jayap\Desktop\2025-2025-Even Semester\BCS401-Analysis and Design of Algorithms\404 Lab Programs\9 Prg\SelectionSort_CSV.c Friday, 18 April, 2025 01:09 PM

plt.plot(data['n'], data['time(ms)'], marker='o', linestyle='-')


plt.title('Selection Sort: Time vs. Number of Elements')
plt.xlabel('Number of Elements (n)')
plt.ylabel('Time Taken (ms)')
plt.grid(True)
plt.show()

OUTPUT: Selection Sort Time Complexity Graph

-5-

You might also like