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

Selection Sort Compiled

The document contains a C program that implements the selection sort algorithm to sort an array of random integers. It generates an array of size 'n' filled with random numbers, sorts them using selection sort, and then displays the sorted array along with the execution time and time complexity. The time complexity of the selection sort is noted as O(n^2).

Uploaded by

vinaybs8vinay8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Selection Sort Compiled

The document contains a C program that implements the selection sort algorithm to sort an array of random integers. It generates an array of size 'n' filled with random numbers, sorts them using selection sort, and then displays the sorted array along with the execution time and time complexity. The time complexity of the selection sort is noted as O(n^2).

Uploaded by

vinaybs8vinay8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

SELECTION SORT PROGRAM

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Function to perform selection sort
void selectionSort(int arr[], int n)
{
int i, j, min_idx,temp;
// One by one move boundary of unsorted subarray
for (i = 0; i<=n-2; i++)
{
// Find the minimum element in unsorted array
min_idx = i;
for (j = i+1; j <=n-1; j++)
{
if (arr[j] <arr[min_idx])
min_idx = j;
}
// Swap the found minimum element with the first element
temp=arr[i];
arr[i]=arr[min_idx];
arr[min_idx]=temp;
}
}
int main()
{
int n ;// Number of elements
printf("enter the value of n\n");
scanf("%d",&n);
int arr[n];
srand(time(NULL)); // Seed for random number generation
// Generate random numbers and fill the array
printf("Original Array: ");
for (int i = 0; i< n; i++)
{
arr[i] = rand() % 10000; // Generating random numbers between 0 and 999
printf("%d ", arr[i]);
}
printf("\n");
// Perform selection sort
double clock_tstart_time = clock();
selectionSort(arr, n);
double clock_tend_time = clock();
double total_time = (double)(clock_tstart_time - clock_tend_time) / CLOCKS_PER_SEC;
// Display sorted array
printf("Sorted Array: ");
for (int i = 0; i< n; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
// Print time complexity
printf("Time Complexity: O(n^2)\n");
printf("Execution Time: %f seconds\n", total_time);
return 0;
}

You might also like