Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.76 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <iomanip>
  4. #include <algorithm>
  5. #include <nmmintrin.h>
  6. #include <set>
  7. #include <map>
  8. #include <unordered_set>
  9. #include <unordered_map>
  10. #include <string>
  11. #include <queue>
  12. #include <cmath>
  13. #include <climits>
  14. #include <bitset>
  15. #include <random>
  16. #include <ctime>
  17. #include <chrono>
  18. #include <cstdio>
  19. #include <cstring>
  20. #include <cassert>
  21. #include <sstream>
  22. using namespace std;
  23.  
  24. void FillInc(int n, int* a);
  25. void FillDec(int n, int* a);
  26. void FillRand(int n, int* a);
  27. void swap(int* xp, int* yp);
  28. void PrintMas(int n, int* a);
  29. void heapify(int* a, int l, int r, int& c, int& m);
  30. void HeapSort(int n, int* a, int& T);
  31. void PrintTabl();
  32.  
  33. int main()
  34. {
  35.     int T = 0;
  36.     int n;
  37.     int* a;
  38.     cin >> n;
  39.     a = new int[n];
  40.     FillRand(n, a);
  41.     PrintMas(n, a);
  42.     cout << endl;
  43.     HeapSort(n, a, T);
  44.     PrintMas(n, a);
  45.     PrintTabl();
  46. }
  47.  
  48. void heapify(int* a, int l, int r, int& c, int& m)
  49. {
  50.     int x = a[l], j;
  51.     int i = l;
  52.     ++m;
  53.     while (i < r/2)
  54.     {
  55.         j = 2 * i;
  56.         ++c;
  57.         if ((j < r) && (a[j + 1] <= a[j]))
  58.             j++;
  59.         ++c;
  60.         if (x <= a[j])
  61.         {
  62.             break;
  63.         }
  64.         ++m;
  65.         a[i] = a[j];
  66.         i = j;
  67.     }
  68.     ++m;
  69.     a[i] = x;
  70. }
  71.  
  72. void HeapSort(int n, int* a, int& T)
  73. {
  74.     int l = n / 2, c = 0, m = 0;
  75.     while (l > 0)
  76.     {
  77.         heapify(a, l, n, c, m);
  78.         --l;
  79.     }
  80.     int r = n ;
  81.     while (r > 1)
  82.     {
  83.         m += 3;
  84.         swap(a[1], a[r]);
  85.         r--;
  86.         heapify(a, 1, r, c, m);
  87.     }
  88.     T = c + m;
  89.  
  90. }
  91.  
  92. void FillInc(int n, int* a)
  93. {
  94.     for (int i = 1; i <= n; ++i)
  95.     {
  96.         a[i] = i;
  97.     }
  98. }
  99.  
  100. void FillDec(int n, int* a)
  101. {
  102.     for (int i = n; i >= 1; i--)
  103.     {
  104.         a[n - i] = i;
  105.     }
  106. }
  107. void FillRand(int n, int* a)
  108. {
  109.     for (int i = 1; i <= n; ++i)
  110.     {
  111.         a[i] = rand() % n;
  112.  
  113.     }
  114. }
  115. void swap(int* xp, int* yp)
  116. {
  117.     int temp = *xp;
  118.     *xp = *yp;
  119.     *yp = temp;
  120. }
  121. void PrintMas(int n, int* a)
  122. {
  123.     for (int i = 1; i < n; ++i)
  124.     {
  125.         cout << a[i] << " ";
  126.     }
  127. }
  128.  
  129. void PrintTabl()
  130. {
  131.     int n = 1, FR = 0, FD = 0, FI = 0;
  132.     int* a = new int[n];
  133.     cout << endl << "|                 HeapSort               |" << endl;
  134.     cout << "| n |  FillInc  |  FillDec  |  FillRand  |" << endl;
  135.  
  136.     for (int n = 100; n <= 500; n += 100)
  137.     {
  138.         int T = 0;
  139.         FillRand(n, a);
  140.         HeapSort(n, a, T);
  141.         FR = T;
  142.         FillInc(n, a);
  143.         HeapSort(n, a, T);
  144.         FI = T;
  145.         FillDec(n, a);
  146.         HeapSort(n, a, T);
  147.         FD = T;
  148.         cout << "|" << n << "|" << setw(11) << FI << "|" << setw(11) << FD << "|" << setw(12) << FR << "|" << endl;
  149.     }
  150.  
  151.     delete[] a;
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement