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
|
using namespace std::chrono;
int RandomNumber2() { return (std::rand() % 100); }
void experiment::FillVector(std::vector<int>& a) {
std::srand(unsigned(std::time(0)));
std::generate(a.begin(), a.end(), RandomNumber2);
std::cout << "\n\nVector: ";
for (std::vector<int>::iterator it = a.begin(); it != a.end(); it++) std::cout << *it << " ";
}
void experiment::StoogeSort(std::vector<int>& a, int start, int end) {
int temp{0};
if (end - start + 1 > 2) {
temp = (end - start + 1) / 3;
StoogeSort(a, start, end - temp);
StoogeSort(a, start + temp, end);
StoogeSort(a, start, end - temp);
}
if (a[end] < a[start]) std::swap(a[end], a[start]);
}
double experiment::measureTime(std::vector<int>& a, int n) {
steady_clock::time_point t1 = steady_clock::now();
StoogeSort(a, 0, n-1);
steady_clock::time_point t2 = steady_clock::now();
duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
double mTime = time_span.count();
return mTime;
}
void experiment::Experiment(std::vector<int> &a, int n, int start, int end) {
FillVector(a);
StoogeSort(a, 0, n - 1);
std::cout << "\n\na. Sorted data: ";
for (int i{ 0 }; i < n; i++) std::cout << a[i] << ' ';
statistics stat;
double* times = new double[10];
double* means = new double[10];
double* stds = new double[10];
std::cout << "\n";
for (int k{0}; k<=53; k++) std::cout << "-";
std::cout << "\nArgument" << " | " << "Average time"<< " | " << "Standard deviation of time |" << std::endl;
for (int k{0}; k<=53; k++) std::cout << "-";
for (n=100; n < 1000; n+=100) {
std::cout << "\n[" << n << "]" << " ";
for (int j{1}; j < 10; j+=1) {
times[j] = measureTime(a, n);
}
means[n] = stat.meanTime(times, n);
stds[n] = stat.stdTime(times, means[n]);
}
}
|