#include <stdlib.h>
#include <time.h>
#include <iostream>
#include "sort2arrays.h"
using std::cin;
using std::cout;
using std::cerr;
using std::endl;
double diffclock(clock_t clock1, clock_t clock2){
double diffticks = clock1 - clock2;
double diffms = (diffticks * 1000) / CLOCKS_PER_SEC;
return diffms;
}
void shuffle1(int* array, int count){
int* randomValues = new int[ count];
for( int i = 0; i < count; i++){
randomValues[i] = rand();
}
sort2Arrays( randomValues, array, count);
delete[] randomValues;
}
void shuffle2(int* array, int count){
int randIndex = -1;
for( int i = 0; i < count; i++){
randIndex = rand() % count;
swap( array[i], array[ randIndex]);
}
}
#define NUM_REPETITIONS 5
int main(int argc, char* argv[]){
clock_t begin, end;
srand( time(NULL));
int maxN = -1;
do{
cout << "Enter the maximum value of N: ";
cin >> maxN;
}while( ! cin );
cerr << "n\tShuffle1\tShuffle2\n";
for(int n = 10; n <= maxN; n *= 10){
cerr << n;
int* array = new int[n];
int timeElapsed = 0;
for( int j = 0; j < NUM_REPETITIONS; j++){
for( int i = 0; i < n; i++){
array[i] = i;
}
begin = clock();
shuffle1( array, n);
end = clock();
timeElapsed += diffclock( end, begin);
}
cerr << '\t' << timeElapsed;
timeElapsed = 0;
for( int j = 0; j < NUM_REPETITIONS; j++){
for( int i = 0; i < n; i++){
array[i] = i;
}
begin = clock();
shuffle2( array, n);
end = clock();
timeElapsed += diffclock( end, begin);
}
cerr << '\t' << timeElapsed;
cerr << '\n';
delete[] array;
}
return 0;
}