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.
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>
// Function to perform selection sort on an array
void selectionSort(int arr[], int n)
{
int i, j, min_idx;
for (i = 0; i < n-1; i++)
{
min_idx = i; // Assume the current element is the minimum
for (j = i+1; j < n; j++)
{
if (arr[j] < arr[min_idx])
{
min_idx = j; // Update min_idx if a smaller element is found
}
}
// Swap the found minimum element with the current element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
// Function to generate an array of random numbers
void generateRandomNumbers(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
arr[i] = rand() % 10000; // Generate random numbers between 0 and 9999
}
}
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
}
// Allocate memory for the array
int *arr = (int *)malloc(n * sizeof(int));
if (arr == NULL)
{
printf("Memory allocation failed\n");
return 1; // Exit if memory allocation fails
}
// Generate random numbers and store them in the array
generateRandomNumbers(arr, n);
// Measure the time taken to sort the array
clock_t start = clock();
selectionSort(arr, n);
clock_t end = clock();
// Calculate and print the time taken to sort the array
double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;
printf("Time taken to sort %d elements: %f seconds\n", n, time_taken);
// Free the allocated memory
free(arr);
return 0;
}
Input:
Enter number of elements: 5050
Enter number of elements: 5100
Enter number of elements: 5150
Enter number of elements: 5200
Enter number of elements: 5250
Output:
Time taken to sort 5050 elements: 0.032293 seconds
Time taken to sort 5100 elements: 0.033048 seconds
Time taken to sort 5150 elements: 0.033641 seconds
Time taken to sort 5200 elements: 0.036172 seconds
Time taken to sort 5250 elements: 0.035274 seconds
Conclusion:
Thus the program was executed sucessfully