Search This Blog

Friday, 5 August 2016

Selection sort

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

using namespace std;

long int n=100000,a[100000];

int main()
{
struct timeval  tv1, tv2;
   
for(long int i=0;i<n;i++)
a[i]=rand();

gettimeofday(&tv1, NULL);

for(long int i=0;i<n;i++)
{
long long int min=a[i],ind=i;
for(long int j=i+1;j<n;j++)
{
if(min>a[j])
{
min=a[j];
ind=j;
}
}
a[ind]=a[i];
a[i]=min;
//cout << a[i] << endl;
}

gettimeofday(&tv2,NULL);
printf("Time taken in execution = %f micro-seconds\n",
    (double) (tv2.tv_usec - tv1.tv_usec));
    return 0;
}


No comments:

Post a Comment