-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap_sort.cpp
67 lines (56 loc) · 1.14 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <vector>
#include <array>
/*
O(n log n)
*/
inline constexpr int LeftChild(const int i)
{
return 2 * i + 1;
}
template<typename Comparable>
void Heapify(int i, const int size, std::vector<Comparable>* const outData)
{
int child;
Comparable tmp;
for(tmp = std::move(outData->at(i)); LeftChild(i) < size; i = child)
{
child = LeftChild(i);
if(child != size - 1 && outData->at(child) < outData->at(child + 1)) // find which child is bigger
{
++child;
}
if(tmp < outData->at(child)) // check if child is bigger than parent
{
outData->at(i) = std::move(outData->at(child));
}
else
{
break;
}
}
outData->at(i) = std::move(tmp);
}
template<typename Comparable>
void HeapSort(std::vector<Comparable>* const outData)
{
for(int i = outData->size() / 2 - 1; i >= 0; --i)
{
Heapify(i, outData->size(), outData);
}
for(int i = outData->size() - 1; i > 0; --i)
{
std::swap(outData->at(0), outData->at(i));
Heapify(0, i, outData);
}
}
int main()
{
std::vector<int> a1{ 8, 6, 4, 4, 1, 9, 2 };
HeapSort(&a1);
for(const auto& x : a1)
{
std::cout << x << ' ';
}
std::cout << '\n';
}