Skip to content

Commit 9c846d7

Browse files
committed
Done heap sort template.
1 parent d9f364e commit 9c846d7

File tree

1 file changed

+80
-13
lines changed

1 file changed

+80
-13
lines changed

template/heap_sort.cpp

Lines changed: 80 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,109 @@
88
#include "algorithm"
99
#include "string"
1010
#include "sstream"
11-
#include <iterator>
11+
#include "iterator"
12+
#include "stack"
13+
#include "tuple"
1214

1315
#define BOOST_IO std::ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
1416
#define ALTER_IN(filename) std::fstream f_in(filename); std::cin.rdbuf(f_in.rdbuf());
1517

1618
using namespace std;
1719

18-
template<typename C>
19-
class MaxHeap{
20+
class MaxHeap {
2021
public:
21-
C *container;
22+
vector<int> container;
23+
int heap_size;
2224

23-
explicit MaxHeap(C container) {
24-
this->container = new C(container);
25+
explicit MaxHeap(const vector<int> &container) : container(container.size() + 1), heap_size(container.size()) {
26+
this->container[0] = -1;
27+
copy_n(begin(container), container.size(), begin(this->container) + 1);
28+
29+
// O(n) Heap Building.
30+
for (int i = (heap_size >> 1); i >= 1; --i) {
31+
max_heapify(i);
32+
}
33+
}
34+
35+
vector<int> sort() {
36+
for (int i = heap_size; i >= 2; --i) {
37+
swap(container[1], container[heap_size]);
38+
39+
// Reduce heap size, then heapify
40+
--heap_size;
41+
max_heapify(1);
42+
}
43+
44+
return vector<int>(begin(container) + 1, end(container));
2545
}
2646

27-
void max_heapify(const int i){
47+
void max_heapify(const int i) {
2848
int l = left(i);
2949
int r = right(i);
3050
int largest = i;
3151

32-
if (l <= this->container->length() && this->container[l] > this->container[i]) {
52+
if (l <= heap_size && container[l] > container[i]) {
3353
largest = l;
3454
}
3555

36-
if (r <= this->container->length() && )
56+
if (r <= heap_size && container[r] > container[largest]) {
57+
largest = r;
58+
}
59+
60+
if (largest != i) {
61+
swap(container[i], container[largest]);
62+
max_heapify(largest);
63+
}
64+
}
65+
66+
void max_heapify__loop(const int i) {
67+
stack<int> s;
68+
s.push(i);
69+
70+
int l, r, largest, current;
71+
72+
while (!s.empty()) {
73+
current = s.top();
74+
s.pop();
75+
76+
tie(l, r) = get_lr(i);
77+
largest = current;
78+
79+
if (l < heap_size && container[l] > container[current]) {
80+
largest = l;
81+
}
82+
83+
if (r < heap_size && container[r] > container[largest]) {
84+
largest = r;
85+
}
86+
87+
if (largest != i) {
88+
swap(container[current], container[largest]);
89+
s.push(largest);
90+
}
91+
}
3792
}
3893

94+
int parent(const int i) { return i >> 1; }
3995

96+
int left(const int i) { return i << 1; }
4097

41-
int parent(const int i){ return i << 1; }
98+
int right(const int i) { return (i << 1) + 1; }
4299

43-
int left(const int i) { return i >> 1; }
100+
tuple<int, int> get_lr(const int i) {
101+
return make_tuple(left(i), right(i));
102+
}
103+
};
44104

45-
int right(const int i) { return (i >> 1) + 1; }
105+
class Test {
106+
public:
107+
explicit Test(vector<int> vec) {
108+
copy(begin(vec), end(vec), ostream_iterator<int>(cout, " "));
109+
cout << endl;
110+
}
46111
};
47112

48-
int main(){
113+
int main() {
49114
BOOST_IO
50115
ALTER_IN("in.txt")
51116

@@ -57,6 +122,8 @@ int main(){
57122

58123
for (auto &row: matrix) {
59124
copy_n(istream_iterator<int>(cin), N, back_inserter(row));
125+
MaxHeap heap(row);
126+
row = heap.sort();
60127
}
61128

62129
for (auto &row: matrix) {

0 commit comments

Comments
 (0)