-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path6-heap-sort.cpp
52 lines (39 loc) · 990 Bytes
/
6-heap-sort.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
#include <bits/stdc++.h>
using namespace std;
int hepify(vector<int>& arr, int n, int node) {
int left = 2 * node + 1;
int right = 2 * node + 2;
int root = node;
if (left < n && arr[left] > arr[root]) {
root = left;
}
if (right < n && arr[right] > arr[root]) {
root = right;
}
if (root != node) {
swap(arr[node], arr[root]);
hepify(arr, n, root);
}
}
void heap_sort(vector<int>& arr) {
for (int i = arr.size() / 2 - 1; i >= 0; i--) {
hepify(arr, arr.size(), i);
}
for (int i = arr.size() - 1; i >= 0; i--) {
swap(arr[i], arr[0]);
hepify(arr, i, 0);
}
}
void test() {
vector<int> testVector({5, 1, 4, 7, 13, 2});
vector<int> sortedVector({1, 2, 4, 5, 7, 13});
heap_sort(testVector);
assert(testVector == sortedVector);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
srand(time(nullptr));
test();
return 0;
}