Program to Sort a given set of elements using Selection sort

				
					#include<stdio.h> 
#include<conio.h> 
#include<time.h>  
void main()   
{
int i,n,j,min,k,a[20],ch=1;    
clock_t begin,end;    
clrscr(); 
while(ch)   
{
printf("\n enter the number of elements\n");       
scanf("%d",&n);       
printf("\n enter the elements to be sorted\n");       
for(i=0;i<n;i++)          
scanf("%d",&a[i]);       
begin=clock();       
for(i=0;i<=n-2;i++)        
{ 
min=i;
delay(200);
for(j=i+1;j<=n-1;j++)
{           
if(a[j]<a[min]) 
min=j;
} 
k=a[i]; 
a[i]=a[min];
a[min]=k;
}
end=clock();
printf("\n\t the sorted list of elements are:\n");
for(i=0;i<n;i++)           
printf("\n%d",a[i]);       
printf("\n\n\t time taken:%lf",(end-begin)/CLK_TCK);
printf("\n\n do u wish to continue (0/1)\n");
scanf("%d",&ch);   
}    
getch();   
}
				
			
OUTPUT

enter the number of elements

5

 enter the elements to be sorted

8

3

5

1

9

 the sorted list of elements are:

 1       3       5       8       9

 time taken:0.824176

Leave a Reply