Page 9 - 4990
P. 9

Лістинг 1. Програма для тестування алгоритму сортування

                   #include "stdafx.h"
                   #include <iostream>
                   #include <algorithm>
                   #include <vector>
                   using namespace std;

                   #include <chrono>
                   using namespace std::chrono;
                   steady_clock::time_point t1, t2;
                   milliseconds time_span;

                   #include "customSort.h"

                   int main()
                   {
                       // random data array
                          const int N = 30000;
                          vector<int> x(N);
                          generate_n(x.begin(), N, rand);

                          // reference sorting (std::sort)
                          vector<int> ref = x;
                          t1 = steady_clock::now();
                          sort(ref.begin(), ref.end());
                          t2 = steady_clock::now();
                          time_span = duration_cast<milliseconds>(t2 - t1);
                          cout << "std::sort: " << time_span.count() << " ms\n";

                          // custom sorting
                          t1 = steady_clock::now();
                          bubble_sort(x);
                          t2 = steady_clock::now();
                          time_span = duration_cast<milliseconds>(t2 - t1);
                          cout << "custom sort: " << time_span.count() << " ms\n";

                          // compare results
                          auto pos = mismatch(x.begin(), x.end(), ref.begin());
                          if (pos.first == x.end())
                          {
                                 cout << "Sorting results are identical.";
                          }
                          else
                          {
                                 size_t index = pos.first - x.begin();
                                 cout << "Sorting result mismatch at index "; << index <<
                   ":\n";
                                 cout << "(std::sort)  \t\t" << *pos.first << endl;


                                                                 9
   4   5   6   7   8   9   10   11   12   13   14