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

DAA Week - 3 - SS

The document describes a C/C++ program to sort a given set of integer elements using selection sort and compute its time complexity. The program generates random integer elements, sorts them using selection sort and records the time taken for different values of n, the number of elements. It then plots a graph of the time taken versus n.

Uploaded by

abhishekbhat2004
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)
53 views2 pages

DAA Week - 3 - SS

The document describes a C/C++ program to sort a given set of integer elements using selection sort and compute its time complexity. The program generates random integer elements, sorts them using selection sort and records the time taken for different values of n, the number of elements. It then plots a graph of the time taken versus n.

Uploaded by

abhishekbhat2004
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/ 2

Bangalore Institute of Technology

Department of Computer Science and Engineering


DESIGN AND ANALYSIS OF ALGORITHMS LAB (BCSL404)
Program 9
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: Sort a given set of elements using Selection sort and determine the time required to sort
elements. Repeat the experiment for different values of n, the number of elements in the list
to be sorted and plot a graph of the time taken versus n.

Definition: selection sort is a sorting routine that scans a list of items repeatedly and, on each
pass, selects the item with the lowest value and places it in its final position. It is based on
brute force approach. Sequential search is a Θ(n2) algorithm on all inputs.

#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() % 1000; // Generating random numbers between 0 and 999
printf("%d ", arr[i]);
}
printf("\n");

// Perform selection sort


clock_t start_time = clock();
selectionSort(arr, n);
clock_t end_time = clock();
double total_time = (double)(end_time - start_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;
}

Output:
Note down the different values of n(eg: 5000,6000,7000,8000,9000,10000) and required
time for all values of n respectively. Plot the graph for the same.

You might also like