Search This Blog

Friday 5 August 2016

Insertion Sort

#include<stdio.h>
#include<math.h>
#include<sys/time.h>
#include<stdlib.h>
#include<iostream>

using namespace std;

int main()
{
long timedif;
struct timeval tpstart;
struct timeval tpend;
struct timeval now;
int rc;

now.tv_sec=000000000;
now.tv_usec=000000;

rc=settimeofday(&now, NULL);

if(rc==0)
{
printf("settimeofday() failed.\n");
}
else
{
printf("\nsettimeofday() Succesful\n");
printf("\nTime Set sec=%ld Msec=%ld\n",now.tv_sec,now.tv_usec);
}

gettimeofday(&tpstart, NULL);

long int n=100000;
long int a[n];

for(long int i=0;i<n;i++)
{
long int temp=random(),flag=1;
for(long int j=0;j<i;j++)
{
if(temp<a[j])
{
for(long int k=i;k>=j;k--)
a[k]=a[k-1];
a[j]=temp;
flag=0;
break;
}
}
if(flag==1)
a[i]=temp;
}

gettimeofday(&tpend, NULL);
timedif = 1000000 * (tpend.tv_sec - tpstart.tv_sec) + tpend.tv_usec - tpstart.tv_usec;

printf("\nTime difference is:%ld\n",timedif);
}


No comments:

Post a Comment