-
Notifications
You must be signed in to change notification settings - Fork 3
/
heapSort.cpp
60 lines (50 loc) · 1.37 KB
/
heapSort.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
#include<iostream>
#include<vector>
#include <utility>
// Swaps the largest child with the parent if parent is smaller than child
// O(log n)
void fix_down(std::vector<int> &vec, int k, int depth) {
// Loop while we have a left child
while (((k + 1) * 2 - 1) < depth) {
int c = (k + 1) * 2 - 1;
// If right child is > left, use that instead
if (c + 1 < depth && vec[c + 1] > vec[c])
++c;
// If parent is > child, we're done
if (vec[k] >= vec[c])
break;
std::swap(vec[k], vec[c]);
k = c;
}
}
// Builds a heap out of a vector
// O(n)
void build_heap(std::vector<int> &vec) {
for (int i = vec.size() - 1; i >= 0; --i) {
fix_down(vec, i, vec.size());
}
}
void heap_sort(std::vector<int> &vec) {
build_heap(vec);
// Now loop through the built heap, "extracting" max by putting in end
// and then fixing
int k = vec.size() - 1;
while (k >= 0) {
std::swap(vec[0], vec[k--]);
fix_down(vec, 0, k + 1);
}
}
int main() {
std::vector<int> vec = { 5, 10, 7, 23, 21 };
std::cout << "Before:" << std::endl;
for (int n : vec) {
std::cout << n << " ";
}
std::cout << std::endl;
heap_sort(vec);
std::cout << "After: " << std::endl;
for (int n : vec) {
std::cout << n << " ";
}
std::cout << std::endl;
}