Advertisement
Guest User

Untitled

a guest
May 24th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <tuple>
  4. #include <algorithm>
  5. #include <fstream>
  6. #include <functional>
  7.  
  8. #include <cmath>
  9. #include <ctime>
  10. #include <random>
  11.  
  12. using namespace std;
  13.  
  14.  
  15. template <class T>
  16. void print(const vector<T> &vec) {
  17.     for (auto e : vec) {
  18.         cout << e << " ";
  19.     }
  20.     cout << "\n";
  21. }
  22.  
  23. template <class T, class ...Args>
  24. unsigned int get_time(const function<T> foo) { // what parameters should be
  25.     auto initial = clock();
  26.  
  27.     foo();
  28.  
  29.     return clock()-initial;
  30. }
  31.  
  32. int main() {
  33.     srandom(time(0));
  34.     int n;
  35.     cin >> n;
  36.     vector<int> vec (n);
  37.     for (unsigned i = 0; i < vec.size(); i++) {
  38.         vec[i] = rand() % (n*3) + 1;
  39.     }
  40.     auto vec2 = vec;
  41.     cout << "initial data:\n";
  42.     print(vec);
  43.     cout << "**************\n";
  44.     auto _heapsort = [&vec] () {
  45.         make_heap(vec.begin(), vec.end());
  46.         sort_heap(vec.begin(), vec.end());
  47.     };
  48.     auto _stdsort = [&vec2] () {
  49.         sort(vec2.begin(), vec2.end());
  50.     };
  51.  
  52.     cout << "heap sort: " << get_time(_heapsort) << "\n";
  53.     cout << "std  sort: " << get_time(_stdsort);
  54.  
  55.  
  56.     return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement