-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathheapify.cpp
63 lines (57 loc) · 1.39 KB
/
heapify.cpp
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Time: O(n)
// Space: O(1)
// Bottom-Up.
class Solution {
public:
/**
* @param A: Given an integer array
* @return: void
*/
void heapify(vector<int> &A) {
for (int i = A.size() / 2; i >= 0; --i) {
sift_down(A, i);
}
}
void sift_down(vector<int>& A, int index) {
while (index < A.size()) {
int smallest = index;
if (index * 2 + 1 < A.size() && A[index * 2 + 1] < A[smallest]) {
smallest = index * 2 + 1;
}
if (index * 2 + 2 < A.size() && A[index * 2 + 2] < A[smallest]) {
smallest = index * 2 + 2;
}
if (smallest == index) {
break;
}
swap(A[smallest], A[index]);
index = smallest;
}
}
};
// Time: O(nlogn)
// Space: O(1)
// Top-Down.
class Solution2 {
public:
/**
* @param A: Given an integer array
* @return: void
*/
void heapify(vector<int> &A) {
for (int i = 0; i < A.size(); ++i) {
sift_up(A, i);
}
}
void sift_up(vector<int>& A, int index) {
while ((index - 1) / 2 >= 0) {
int parent = (index - 1) / 2;
if (A[index] < A[parent]) {
swap(A[parent], A[index]);
index = parent;
} else {
break;
}
}
}
};