Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- class Pyramid {
- int n;
- vector<int> arr;
- void siftDown(int root, int bottom) {
- int maxChild;
- bool done = false;
- while ((root * 2 <= bottom) && (!done)) {
- if (root * 2 == bottom || arr[root * 2] > arr[root * 2 + 1]) {
- maxChild = root * 2;
- } else {
- maxChild = root * 2 + 1;
- }
- if (arr[root] < arr[maxChild]) {
- swap(arr[root], arr[maxChild]);
- root = maxChild;
- } else {
- done = true;
- }
- }
- }
- public:
- explicit Pyramid(const vector<int> &_arr) {
- arr = _arr;
- n = _arr.size();
- }
- void sort() {
- for (int i = (n / 2); i >= 0; i--)
- siftDown(i, n - 1);
- for (int i = n - 1; i >= 1; i--) {
- swap(arr[0], arr[i]);
- siftDown(0, i - 1);
- }
- }
- vector<int> getSorted() {
- return arr;
- }
- };
- int main() {
- int n;
- cin >> n;
- vector<int> v(n);
- for (int &i: v)
- cin >> i;
- Pyramid pyramid(v);
- pyramid.sort();
- v = pyramid.getSorted();
- for (int &i: v)
- cout << i << ' ';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement