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

Expt 9 Selection Sort ADA Lab

The document describes a C/C++ program that implements the Selection Sort algorithm to sort a set of integer elements. It includes functionality to generate random numbers, measure the time taken for sorting, and print the sorted array. The program is designed to be tested with varying values of n greater than 5000, with results plotted to show time complexity.

Uploaded by

prathamk22301
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)
6 views2 pages

Expt 9 Selection Sort ADA Lab

The document describes a C/C++ program that implements the Selection Sort algorithm to sort a set of integer elements. It includes functionality to generate random numbers, measure the time taken for sorting, and print the sorted array. The program is designed to be tested with varying values of n greater than 5000, with results plotted to show time complexity.

Uploaded by

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

ADA Lab

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.

#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;
for (i = 0; i < n - 1; i++) {
min_idx = i;
for (j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx]) {
min_idx = j;
}
}
// Swap the found minimum element with the first element
temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}

// Function to print the array


void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

// main function
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);

int arr[n];

// Seed for random number generation


srand(time(0));

// Generate random numbers


printf("Generated array: \n");
for (int i = 0; i < n; i++) {
arr[i] = rand() % 1000; // Random numbers between 0 and 999
printf("%d ", arr[i]);
}
printf("\n");
// Measure time taken by selection sort
clock_t start, end;
start = clock();
selectionSort(arr, n);
end = clock();
// Calculate time taken
double time_taken = ((double)(end - start)) / CLOCKS_PER_SEC;

printf("\nSorted array: \n");


printArray(arr, n);
printf("\nSelection Sort took %f seconds to sort %d elements.\n", time_taken, n);
return 0;
}

Output-1:

Enter the number of elements: 10

Generated array:

445 881 472 724 42 918 28 538 248 437

Sorted array:

28 42 248 437 445 472 538 724 881 918

Selection Sort took 0.000002 seconds to sort 10 elements.’

Output-2

Enter the number of elements: 100

Generated array:

873 67 725 144 80 459 156 36 4 130 284 929 404 759 699 775 92 27 179 679 404 839 321 43 989 419
758 369 765 151 523 990 570 601 134 651 60 642 39 64 124 323 994 528 435 693 655 527 72 834 558
828 673 879 872 14 299 630 383 416 133 907 406 56 508 892 59 568 535 98 984 659 773 978 540 208
23 195 87 447 30 645 276 55 525 500 70 176 482 453 944 616 712 702 24 572 594 83 492 129

Sorted array:

4 14 23 24 27 30 36 39 43 55 56 59 60 64 67 70 72 80 83 87 92 98 124 129 130 133 134 144 151 156 176


179 195 208 276 284 299 321 323 369 383 404 404 406 416 419 435 447 453 459 482 492 500 508 523
525 527 528 535 540 558 568 570 572 594 601 616 630 642 645 651 655 659 673 679 693 699 702 712
725 758 759 765 773 775 828 834 839 872 873 879 892 907 929 944 978 984 989 990 994

Selection Sort took 0.000022 seconds to sort 100 elements.

You might also like