0

I am trying to time the execution of a quicksort algorithm in C++ but for some reason it constantly returns 0 for me, which clearly can't be right!

I don't know where I am gooing wrong, so any help would be greatly appreciated!I have the code done for arrays of size 10,50,100,500 and 1000 but since the code is similart for them all I will just give the code for the arrays of size 10 and 50.

```
#include <iostream>
#include <time.h>
#include <cstdlib>
using namespace std;
void quickSort(int arr[],int left,int right) { /*This is the quickSort function that will sort all of the elements in an array in ascending order*/
int i=left,j=right;
int tmp;
int pivot = arr[(left + right) / 2];
/* partition */
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
};
/* recursion */
if (left < j)
quickSort(arr, left, j);
if (i < right)
quickSort(arr, i, right);
}
void time(int arr[],int left,int right){
clock_t stopwatch,t1,t2; /*These 3 lines are timing the exection of the quicksort to take place*/
t1=clock();
quickSort(arr,left,right);
t2=clock(); /*Used to time the quicksort*/
cout<<"t1 = "<<t1<<endl;
cout<<"t2 = "<<t2<<endl;
stopwatch=t2-t1; /*How long it took to execute the quicksort*/
cout<< "Stopwatch ="<<stopwatch<<endl; /*Displaying how long it took to execute the code-i.e.how long the quickSort function took*/
}
int main()
{
int arraya[10],arrayb[50],i;
srand(time(NULL)); /*Seed for a random number generator*/
for(i=0;i<10;i++)
arraya[i]=rand();
time(arraya,0,9);
cout<<"The sorted array of 10 elements is :\n";
for(i=0;i<10;i++){
cout<<"arraya ["<<i<<"] = "<<arraya[i]<<"\n";}
srand(time(NULL)); for(i=0;i<50;i++)
arrayb[i]=rand();
time(arrayb,0,49);
cout<<"The sorted array of 50 elements is :\n";
for(i=0;i<50;i++){
cout<<"arrayb ["<<i<<"] = "<<arrayb[i]<<"\n";}
return 0;
}
```

Thanks in advance.

*Edited 5 Years Ago by WaltP*: Added CODE Tags. Please use them.