I was wondering if there were away to repeat the same test multiple times and average the time.
The code works fine
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
void PrintArray(int* array, int n);
void QuickSort(int* array, int start, int end);
int partition(int* array, int pivot, int start, int end);
void swap(int &temp, int &tmp2);
double diffclock(clock_t clock1,clock_t clock2)
{
double diffticks=clock1-clock2;
double diffms=(diffticks*10)/CLOCKS_PER_SEC;
return diffms;
}
int main(void)
{
int ARRAY_SIZE;
cout<< "How many random numbers would you like generated? " << endl;
cin >> ARRAY_SIZE;
int* array = new int[ARRAY_SIZE];
srand((unsigned)time(0));
for(int i=0; i<ARRAY_SIZE; i++){
array[i] = (rand()%100000)+1;
}
PrintArray(array, ARRAY_SIZE);
clock_t begin=clock();
QuickSort(array,0,ARRAY_SIZE - 1);
cout<<endl<<"The list has been sorted, now it is : "<<endl;
PrintArray(array, ARRAY_SIZE);
clock_t end=clock();
cout<<"Execution time: "<<diffclock(end,begin)<<" ms."<<endl;
cin.get();
cin.get();
system("PAUSE");
delete[] array;
return 0;
}
void swap(int &temp, int &tmp2)
{
int tmp;
tmp = temp;
temp = tmp2;
tmp2 = tmp;
}
void PrintArray(int* array, int n)
{
int i;
for( i = 0; i < n; i++) cout<<array[i]<<'\t';
}
void QuickSort(int* array, int start, int end)
{
int pivot = array[start];
int middle;
if(start < end)
{
middle = partition(array, pivot, start, end);
array[middle] = pivot;
QuickSort(array, start, middle-1);
QuickSort(array, middle+1, end);
}
}
int partition(int* array, int pivot, int start, int end)
{
int leftLimit = start;
int rightLimit = end;
while(leftLimit < rightLimit)
{
while( pivot < array[rightLimit]
&& rightLimit > leftLimit)
{
rightLimit--;
}
swap(array[leftLimit], array[rightLimit]);
while( pivot >= array[leftLimit]
&& leftLimit < rightLimit)
{
leftLimit++;
}
swap(array[rightLimit], array[leftLimit]);
}
return leftLimit;
//leftBoundary and rightBoundary are equal
}