0% found this document useful (0 votes)
10 views

Program 9

Code
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Program 9

Code
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

EXERCISE NO.

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.

PROGRAM:

#include<stdio.h>
#include<time.h>

void selsort(int a[], int n)


{
int i, j, small, pos, temp;
for(j = 0; j < n - 1; j++)
{
small = a[j];
pos = j;
for(i = j + 1; i < n; i++)
{
if(a[i] < small)
{
small = a[i];
pos = i;
}
}
temp = a[j];
a[j] = small;
a[pos] = temp;
}
}

int main()
{
int a[10], i, n;
struct timespec start, end;
double dura;

printf("\nEnter the n value:");


scanf("%d", &n);
printf("\nEnter the array:");
for(i = 0; i < n; i++)
scanf("%d", &a[i]);

clock_gettime(CLOCK_MONOTONIC, &start);
selsort(a, n);
clock_gettime(CLOCK_MONOTONIC, &end);

dura = (end.tv_sec - start.tv_sec) + (end.tv_nsec - start.tv_nsec) / 1e9;

printf("\nTime taken is: %lf seconds", dura);


printf("\nSorted array is:");
for(i = 0; i < n; i++)
printf("%d ", a[i]);

return 0;
}

OUTPUT:

You might also like