[go: up one dir, main page]

0% found this document useful (0 votes)
8 views8 pages

Selection Sort

The document provides a C program that implements the selection sort algorithm, allowing users to sort an array of random integers. It includes functionalities to generate random arrays, display them, and plot sorting time data to a file. The program also offers a menu for users to choose between plotting data, performing selection sort, or exiting the application.

Uploaded by

atteezz.123
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)
8 views8 pages

Selection Sort

The document provides a C program that implements the selection sort algorithm, allowing users to sort an array of random integers. It includes functionalities to generate random arrays, display them, and plot sorting time data to a file. The program also offers a menu for users to choose between plotting data, performing selection sort, or exiting the application.

Uploaded by

atteezz.123
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/ 8

Selection sort C program

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <time.h>

// Swap function
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

// Selection Sort
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int min = i;
for (int j = i + 1; j < n; j++)
if (arr[j] < arr[min])
min = j;
swap(&arr[i], &arr[min]);
}
}
// Generate random input
void generateRandomArray(int arr[], int n) {
for (int i = 0; i < n; i++)
arr[i] = rand() % 10000;
}

// Display array
void displayArray(int arr[], int n) {
for (int i = 0; i < n; i++)
printf("%5d\n", arr[i]);
}

int main() {
srand(time(NULL));
int choice, n, arr[500000];
struct timeval tv;
double start, end;
FILE *fp;

while (1) {
printf("\n1. Plot Graph\n2. Selection Sort\n3. Exit\nEnter choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
fp = fopen("sPlot.dat", "w");
if (!fp) {
printf("File error!\n");
break;
}
for (n = 5000; n <= 50000; n += 1000) {
generateRandomArray(arr, n);
gettimeofday(&tv, NULL);
start = tv.tv_sec + tv.tv_usec / 1000000.0;
selectionSort(arr, n);
gettimeofday(&tv, NULL);
end = tv.tv_sec + tv.tv_usec / 1000000.0;
fprintf(fp, "%d\t%lf\n", n, end - start);
}
fclose(fp);
printf("Data saved to 'sPlot.dat'.\n");
break;

case 2:
printf("Enter number of elements: ");
scanf("%d", &n);
generateRandomArray(arr, n);
printf("\nUnsorted Array:\n");
displayArray(arr, n);
selectionSort(arr, n);
printf("\nSorted Array:\n");
displayArray(arr, n);
break;

case 3:
return 0;

default:
printf("Invalid choice! Try again.\n");
}
}
}
Output
Command 1:gedit sel.c
Command 2: gcc -o sel sel.c
Command 3: ./sel
Enter your choice
3
Command :gnuplot
G N U P L O T Version 5.4 patchlevel 2 last modified 2021-06-01 Copyright
(C) 1986-1993, 1998, 2004, 2007-2021 Thomas Williams, Colin Kelley and
many others gnuplot home: http://www.gnuplot.info faq, bugs, etc: type "help
FAQ" immediate help: type "help" (plot window: hit 'h') Terminal type is now
'qt'
gnuplot> plot './sPlot.dat' using 1:2 with linespoints

You might also like