Measure elapsed time for Bubble sort algorithm for multiple array sizes N.

Why is it outputting 0 as the time elapsed?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  #include <iostream>
#include <time.h>
#include <ctime>
using namespace std;
double BubbleSort(int[], int );

void main(){
	int M,J,asize=50;
	double etime;
	int Array[50];
	//Array=new int[asize];
	
	for (M=0;M<asize;M++)
	{
		Array[M]=rand();
	}
	for (J=10;J<asize;J=J+10)
	{
		
		etime=BubbleSort(Array,J);
		cout<<"Random numbers sorted: \t"; 
		for(int i=0; i<J; i++)
		{
			cout<< Array[i]<<" "; 
		}
		cout<<endl;
		cout<<"Time Taken:"<<etime<<"s"<<endl<<endl;
	}
}

double BubbleSort(int Array[], int n)
{
	clock_t startClock,finishClock;
	int buf;
	double etime;
	startClock = clock();
	for(int i=0;i<n-1;i++ )
	{
		if (Array[i]>Array[i+1])
				{ 
					 
					buf = Array[i+1]; 
					Array[i+1] = Array[i]; 
					Array[i] = buf; 
				}
		}
	finishClock = clock();
	return (etime=(finishClock-startClock));
}




	


Last edited on
Use the more accurate C++11 <chrono> library:
http://en.cppreference.com/w/cpp/chrono/time_point#Example
Topic archived. No new replies allowed.