Tuesday 24 January 2012

Selection Sort


/*Author-Neelkant.S.Patil,GMIT,Davanagere*/
//Selection Sort
#include<stdio.h>
#include<conio.h>
#include<time.h>
void selection_sort(int a[],int n)
{
int i,j,pos,temp;
for(i=0;i<n-1;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
delay(100);
if(a[j]<a[pos])
pos=j;
}
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
}
void main()
{
int n,i,a[50];
clock_t begin,end;
clrscr();
printf("Enter the number of items\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();
selection_sort(a,n);
end=clock();
printf("\n The sorted elements are: \n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
printf("Begin time=%d\n",begin);
printf("End time=%d\n",end);
printf("Total number of ticks=%d\n",(end-begin));
printf("Total time required=%f\n",((end-begin)/CLK_TCK));
getch();
}

No comments:

Post a Comment