Search This Blog

Friday 5 August 2016

Bubble 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++)
     {
           for(long int j=i;j<n;j++)
           {
                 if(a[i]>a[j])
                 {
                        long int temp;
                        temp=a[i];
                        a[i]=a[j];
                        a[j]=temp;
                  }
           }
     }

     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