Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. vector<int> generate(int n, int type) {
  6.     vector<int> ans(n, 0);
  7.     if (type == 0) {
  8.         for (int i=0; i<n; i++) {
  9.             ans[i] = i;
  10.         }
  11.     }
  12.     if (type == 1) {
  13.         for (int i=0; i<n; i++) {
  14.             ans[i] = n - i - 1;
  15.         }
  16.     }
  17.     if (type == 2) {
  18.         srand(time(0));
  19.         for (int i=0; i<n; i++) {
  20.             ans[i] = rand() % (n * 5);
  21.         }
  22.     }
  23.     return ans;
  24. }
  25.  
  26. /*
  27.  * first = comps cnt
  28.  * second = swaps cnt
  29.  */
  30. pair<int, int> comb(vector<int>& v) {
  31.     int swaps = 0;
  32.     int comps = 0;
  33.     double factor = 1.25;
  34.     double step = v.size() - 1;
  35.  
  36.     clock_t time;
  37.     time = clock();
  38.  
  39.     while (step >= 1) {
  40.         for (int i=0; i<v.size()-step; i++) {
  41.             comps++;
  42.             if (v[i] > v[i+step]) {
  43.                 swap(v[i], v[i+step]);
  44.                 swaps++;
  45.             }
  46.         }
  47.         step /= factor;
  48.     }
  49.     for (long long i=0; i<(long long)(v.size() * v.size()); i++) {}
  50.  
  51.     long double tm = (long double)(clock() - time)/CLOCKS_PER_SEC;
  52.  
  53.     cout << "size: " << v.size() << endl << "Time for sort  : " << setprecision(10) << fixed << tm << " seconds" << endl;
  54.  
  55.     return pair<int, int>({comps, swaps});
  56. }
  57.  
  58. int main(void)
  59. {
  60.     /*vector<int> v = generate(10, 2);
  61.     pair<int, int> standart_10 = comb(v);
  62.     v = generate(100, 2);
  63.     pair<int, int> standart_100 = comb(v);
  64.     v = generate(1000, 2);
  65.     pair<int, int> standart_1000 = comb(v);
  66.     v = generate(5000, 2);
  67.     pair<int, int> standart_5000 = comb(v);
  68.     v = generate(10000, 2);
  69.     pair<int, int> standart_10000 = comb(v);
  70.     v = generate(20000, 2);
  71.     pair<int, int> standart_20000 = comb(v);
  72.     v = generate(50000, 2);
  73.     pair<int, int> standart_50000 = comb(v);
  74.  
  75.     cout << "comps: " << standart_10.first << " " << "swaps: " << standart_10.second << endl;
  76.     cout << "comps: " << standart_100.first << " " << "swaps: " << standart_100.second << endl;
  77.     cout << "comps: " << standart_1000.first << " " << "swaps: " << standart_1000.second << endl;
  78.     cout << "comps: " << standart_5000.first << " " << "swaps: " << standart_5000.second << endl;
  79.     cout << "comps: " << standart_10000.first << " " << "swaps: " << standart_10000.second << endl;
  80.     cout << "comps: " << standart_20000.first << " " << "swaps: " << standart_20000.second << endl;
  81.     cout << "comps: " << standart_50000.first << " " << "swaps: " << standart_50000.second << endl;*/
  82.  
  83.  
  84.     freopen("output.txt", "w", stdout);
  85.  
  86.     vector<int> v = generate(10000, 2);
  87.     pair<int, int> standart_10 = comb(v);
  88.     v = generate(20000, 2);
  89.     pair<int, int> standart_100 = comb(v);
  90.     v = generate(50000, 2);
  91.     pair<int, int> standart_1000 = comb(v);
  92.     v = generate(100000, 2);
  93.     pair<int, int> standart_10000 = comb(v);
  94.  
  95.     return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement