Analysis of Algorithm (AOA) / Data Structures (DS)

Selection Sort in C using Random Numbers

Author: Bhumik Dedhia
/*
	Program to perform Selection Sort in C using Random Numbers
	Author: Bhumik Dedhia	Author Link: https://www.facebook.com/bhumik2107
	www.pracspedia.com
*/
#include<stdio.h>
#include<conio.h>
#include<time.h>
void select(int x[],int n);
void main()
{
	time_t start,end;
	int x[50];
	int n,i;
	clrscr();
	start=time(NULL);
	printf("Enter the no of elements:\n");
	scanf("%d",&n);
	printf("The unsorted list is:\n");
	for(i=0;i < n;i++)
	{
		x[i]=rand();
		printf("%d ", x[i]);
	}
	select(x,n);
	printf("\nThe sorted list is\n");
	for(i=0;i < n;i++)
		printf("%d ",x[i]);
	end=time(NULL);
	printf("the time is:%f\n",difftime(end,start));
	getch();
}

void select(int x[],int n)
{
	int index,max,j,i,count=0;
	for(i=n-1;i > 0;i--)
	{
		max=x[0];
		count++;
		index=0;
		count++;
		for(j=1;j <= i;j++)
		{
			if(x[j] > max)
			{
				max=x[j];
				count++;
				index=j;
				count++;
			}   
			count++;    
		}
		x[index]=x[i];
		count++;
		x[i]=max;
		count++;
	}
}

								
Download Source Code DS Program List AOA Program List