#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
void insertion(int [],int);
void main()
{
int
a[5],i,n=5;
int t1,t2;
struct
timeval tt;
struct
timezone tz;
printf("Enter
5 element: ");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
gettimeofday(&tt,&tz);
t1=tt.tv_usec;
insertion(a,n);
gettimeofday(&tt,&tz);
t2=tt.tv_usec;
printf("\n\nTime
required= %d",t2-t1);
}
void insertion(int a[],int n)
{
int
i,j,temp;
for(i=0;i<n;i++)
{
temp=a[i];
for(j=i-1;j>=0;j--)
{
if(a[j]>temp)
a[j+1]=a[j];
else
break;
}
a[j+1]=temp;
}
for(i=0;i<n;i++)
printf("%d
",a[i] );
}
Output :-
No comments:
Post a Comment