User:Typetive/Template:Check sort C++ code
Jump to navigation
Jump to search
#include <iostream>
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include <math.h>
inline double Timer() { return ((double)clock())/CLOCKS_PER_SEC; }
inline double Rnd() { return ((double)rand())/RAND_MAX; }
int main() {
cout << "Please wait for the RESULTS: (Check sort) is working... " << endl;
long* a = 0L;
long* b = 0L;
double start1, start, finish2, finish1, finish;
const int one = 1L;
const int zero = 0L;
const long maxi = 10000000L;
const long topnum = maxi;
const long botnum = one;
long i = 0L, n = 0L, j = 0L, k = maxi;
ofstream out("checksort.txt");
out << endl;
out.flush();
delete[] a;
delete[] b;
a = new long[k];
b = new long[k];
start1 = Timer();
for(k=botnum; k <= topnum; k += topnum/100) {
start = Timer();
for (i=botnum; i <= k; i++) {
n = ((int)(k * Rnd())) + botnum;
a[n] = one; // (check sort is a(n) = one)
}
finish1 = Timer() - start;
start = Timer();
j = 0L;
for (n = botnum; n <= k; n++) {
if (a[n] > zero) { b[j] = n; j = j + one; }
}
finish2 = Timer() - start;
finish = Timer() - start1;
out << k << "\t" << finish1 << "\t" << finish2 << endl;
out.flush();
}
out.close();
cout << endl << endl;
cout << "1.) Total number of items sorted:...................... " << j << endl;
cout << "2.) Total primary array size to accomodate the sort:... " << maxi << endl;
cout << "3.) Total secondary array size to accomodate the sort:. " << j << endl;
cout << "4.) Total length of key:............................... " <<
( k == 0 ? 0 : (int) ceil( log( (double) k )/log( 10.0 ) ) ) << " decimal digits." << endl;
cout << "5.) Total time for setup per 100,000 items:............ " << finish1 << " seconds." << endl;
cout << "6.) Total time for sort per 100,000 items:............. " << finish2 << " seconds." << endl;
cout << "7.) Total time for program:............................ " << finish << " seconds." << endl;
cout << endl << endl;
/*
for (i=1; i<=j; i++) {
cout << i << b[i] << endl;
}
*/
return 0;
}