#include<stdio.h>
#include<sys/time.h>
#define max 20
void selection(int [],int);
void main()
{
int
a[max],i,n;
int t1,t2;
struct
timeval tt;
struct
timezone tz;
printf("How
many element you want to short: ");
scanf("%d",&n);
printf("Enter
array: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
gettimeofday(&tt,&tz);
t1=tt.tv_usec;
selection(a,n);
gettimeofday(&tt,&tz);
t2=tt.tv_usec;
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n\nTime
required= %d",t2-t1);
}
void selection(int a[],int n)
{
int
i,j,t,min;
for(i=0;i<n;i++)
{
min=i;
for(j=i+1;j<n;j++)
{
if(a[j]<a[min])
min=j;
}
t=a[i];
a[i]=a[min];
a[min]=t;
}
}
Output :-
No comments:
Post a Comment