Lab9 Selection Sort
Lab9 Selection Sort
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.
Aim:
To sort a given set of n integer elements using selection sort and plot the graph between time and different number of
Values (n > 5000).
Algorithm:
ALGORITHM SelectionSort(A[0..n - 1])
//Sorts a given array by selection sort
//Input: An array A[0..n - 1] of orderable elements
//Output: Array A[0..n - 1] sorted in nondecreasing order
for i ←0 to n - 2 do
min ←i
for j ←i + 1 to n - 1 do
if A[j ] <A[min] min ←j
o swap A[i] and A[min]
Program:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int n;
printf("Enter number of elements: ");
scanf("%d", &n); // Read the number of elements from the user
if (n <= 5000)
{
printf("Please enter a value greater than 5000\n");
return 1; // Exit if the number of elements is not greater than 5000
}
Conclusion:
Thus the program was executed sucessfully