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

Selection Sort

Uploaded by

Ms Anushree G
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)
3 views3 pages

Selection Sort

Uploaded by

Ms Anushree G
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

#include <stdio.

h>

#include <stdlib.h>

#include <time.h>

// Function to perform selection sort

void selectionSort(int arr[], int n) {

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

int min_idx = i;

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

if (arr[j] < arr[min_idx])

min_idx = j;

// Swap the found minimum element with the first element

int temp = arr[min_idx];

arr[min_idx] = arr[i];

arr[i] = temp;

// Function to print the array

void printArray(int arr[], int size) {

for (int i = 0; i < size; i++) {

printf("%d ", arr[i]);

printf("\n");

int main() {

FILE *fptr;

fptr = fopen("selection_sort_results.csv", "w");

if (fptr == NULL) {
printf("Error opening file!\n");

return 1;

srand(time(0));

fprintf(fptr, "Number of Elements,Time Taken (seconds)\n");

for (int n = 5000; n <= 50000; n += 5000) {

int *arr = (int *)malloc(n * sizeof(int));

if (arr == NULL) {

printf("Memory not allocated.\n");

exit(0);

for (int i = 0; i < n; i++) {

arr[i] = rand();

clock_t start, end;

double cpu_time_used;

start = clock();

selectionSort(arr, n);

end = clock();

cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;

printf("Time taken to sort %d elements: %f seconds\n", n, cpu_time_used);

fprintf(fptr, "%d,%f\n", n, cpu_time_used);

free(arr);
}

fclose(fptr);

return 0;

You might also like