-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxHeap.cpp
More file actions
29 lines (28 loc) · 740 Bytes
/
Copy pathMaxHeap.cpp
File metadata and controls
29 lines (28 loc) · 740 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<bits/stdc++.h>
using namespace std;
void heapify(int index, vector<int>& numbers) {
int left = (2*index) + 1;
int right = (2*index) + 2;
int maxIndex = index;
if(left < numbers.size() && numbers[left] > numbers[maxIndex]) {
maxIndex = left;
}
if(right < numbers.size() && numbers[right] > numbers[maxIndex]) {
maxIndex = right;
}
if(maxIndex != index) {
swap(numbers[maxIndex], numbers[index]);
heapify(maxIndex, numbers);
}
}
int main() {
vector<int> numbers{7, 8, 3, 2, 1, 9};
for(int i=(numbers.size()/2)-1; i >= 0; --i) {
heapify(i, numbers);
}
for(int x : numbers) {
cout << x << " ";
}
cout << "\n";
return 0;
}