Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define cin fin
  5. #define cout fout
  6. #define sp << " " <<
  7. #define nl << "\n"
  8.  
  9. ifstream fin("prob5.in");
  10. ofstream fout("prob.out");
  11.  
  12. int n, i, j, h[100], k, a;
  13.  
  14. void insert(int a) {
  15.     n++;
  16.     k++;
  17.     h[k] = a;
  18. }
  19.  
  20. void aranjeaza(int nn) {
  21.     int x;
  22.     x = h[nn];
  23.     while (nn > 1 && x > h[nn / 2]) {
  24.         h[nn] = h[nn / 2];
  25.         nn = nn / 2;
  26.     }
  27.     h[nn] = x;
  28. }
  29.  
  30. void coboara(int n) {
  31.     int fiu = -1;
  32.     while (fiu != 0) {
  33.         if (2 * n <= k)
  34.             fiu = 2 * n;
  35.         if (2 * n + 1 <= k && h[2 * n + 1] > h[fiu])
  36.             fiu = 2 * n + 1;
  37.  
  38.  
  39.         if (h[fiu] <= h[n])
  40.             fiu = 0;
  41.         if (fiu) {
  42.             swap(h[n], h[fiu]);
  43.             n = fiu;
  44.         }
  45.     }
  46. }
  47.  
  48.  
  49. void sterg() {
  50.     if (k < 1) return;
  51.     cout << h[1] << " ";
  52.     h[1] = h[k];
  53.     k--;
  54.     n--;
  55.     coboara(1);
  56. }
  57.  
  58. void cobor(int a[], int n, int i) {
  59.     int rad = i;
  60.     int st = 2 * i + 1;
  61.     int dr = 2 * i + 2;
  62.     if (st < n && a[st] < a[rad])
  63.         rad = st;
  64.     if (dr < n && a[dr] < a[rad])
  65.         rad = dr;
  66.  
  67.     if (rad != i) {
  68.         swap(a[i], a[rad]);
  69.         cobor(a, n, rad);
  70.     }
  71. }
  72.  
  73. void heapsort(int a[], int n) {
  74.     for (int i = n / 2 - 1; i >= 0; i--)
  75.         cobor(a, n, i);
  76.  
  77.     for (int i = n - 1; i >= 0; i--) {
  78.         swap(a[0], a[i]);
  79.         cobor(a, i, 0);
  80.     }
  81. }
  82.  
  83. int main() {
  84.     int t;
  85.     k = 0;
  86.     while (cin >> t) {
  87.         insert(t);
  88.         aranjeaza(k);
  89.     }
  90.  
  91.     cout << "Avem max-heapul: " << endl;
  92.     for (i = 1; i <= k; i++)
  93.         cout << h[i] << " ";
  94.     cout nl nl;
  95.  
  96.  
  97.     cout << "Max1: " << h[1] << " " nl;
  98.     sterg();
  99.     cout << "Max2: " << h[1] << " " nl;
  100.     sterg();
  101.     cout << "Max2: " << h[1] << " " nl;
  102.     sterg();
  103.  
  104.     if (k == 0) {
  105.         cout << "Heapul este gol!" nl;
  106.     } else {
  107.  
  108.         cout << "Avem max-heapul modificat: " << endl;
  109.         for (i = 1; i <= k; i++)
  110.             cout << h[i] << " ";
  111.         cout nl nl;
  112.  
  113.         heapsort(h, n+1);
  114.  
  115.         cout << "Heapul sortat descresc:\n";
  116.         for (i = 1; i <= k; i++)
  117.             cout << h[i] << " ";
  118.         cout << endl;
  119.     }
  120.  
  121.  
  122.  
  123.     return 0;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement