Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define N 1000
- int mas[N];
- void swapp(int i, int j) {
- int x = mas[i];
- mas[i] = mas[j];
- mas[j] = x;
- }
- void siftUp(int i) {
- if (i == 1) return;
- int p = i / 2;
- if (mas[p] < mas[i]) {
- swapp(p, i);
- siftUp(p);
- }
- }
- void siftDown(int i,int n) {
- int l = 2 * i;
- int r = 2 * i + 1;
- int max = i;
- if (max > n + 1) return;
- if (l <= n && mas[max] < mas[l]) max = l;
- if (r <= n && mas[max] < mas[r]) max = r;
- if (max != i) {
- swapp(max, i);
- siftDown(max, n);
- }
- }
Add Comment
Please, Sign In to add comment