Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- void rep(vector <int>& v, int p, int top)
- {
- int l = 2 * p + 1;
- int r = 2 * p + 2;
- if (top <= l)
- return;
- if (top == l + 1)
- r = l;
- int imax = v[l] > v[r] ? l : r;
- if (v[imax] > v[p])
- swap(v[imax], v[p]);
- rep(v, imax, top);
- }
- int main()
- {
- vector <int> a;
- for (int i = 0; i < 10; i++)
- {
- a.push_back(i);
- cout << a[i] << " ";
- }
- cout << endl;
- for (int i = (int)a.size() / 2; i >= 0; i--)
- rep(a, i, (int)a.size());
- for (int i = 0; i < 10; i++)
- cout << a[i] << " ";
- cout << endl;
- for (int i = (int)a.size() - 1; i > 0; i--)
- {
- swap(a[0], a[i]);
- rep(a, 0, i);
- }
- for (int i = 0; i < 10; i++)
- cout << a[i] << " ";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement