-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMedian Maintenance
309 lines (280 loc) · 5.72 KB
/
Median Maintenance
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//Implementation of a heap
#include<iostream>
#include<vector>
#include<fstream>
#include<sstream>
#include<string>
#include<chrono>
using namespace std;
class Heap_max {
private:
vector<int>list;
public:
Heap_max() {
list = {};
}
void insert(int a) {
list.push_back(a);
heapify(a);
}
void heapify(int val) {//From bottom to top
int n = list.size();
int j = n;
if (n > 1) {
while (j > 1 && !check_parent(val, j)) {
j = bubble_up(val, j);
}
}
}
bool check_parent(int k, int pos) { //parent should be greater than child
int parent = pos / 2;
if (list[parent - 1] < k) {
return 0;
}
return 1;
}
int bubble_up(int k, int pos) { //if child greater than parent
int parent = pos / 2;
list[pos - 1] = list[parent - 1];
list[parent - 1] = k;
return parent;
}
int extract_max() {
int max = list[0];
int n = list.size();
list[0] = list[n - 1];
//list[n - 1] = max;
list.pop_back();
int k = list[0];
int pos = 1;
if (n > 2) {
while ((2 * pos)+1 < n && !check_child(k, pos)) {
pos = bubble_down(k, pos);
}
}
return max;
}
bool check_child(int k, int pos) { //child should be less than parent
int n = list.size();
int pos1 = 2 * pos;
int pos2 = (2 * pos) + 1;
if (pos1 < n) {
int child1 = list[pos1 - 1];
if (child1 > k) {
return 0;
}
}
if (pos2 < n) {
int child2 = list[pos2 - 1];
if ( child2 > k) {
return 0;
}
}
return 1;
}
int bubble_down(int k, int pos) { //if parent is less than child
int pos1 = 2 * pos;
int pos2 = 2 * pos + 1;
int child1 = list[pos1 - 1];
int child2 = list[pos2 - 1];
if (child1 >= child2) {
list[pos - 1] = list[pos1 - 1];
list[pos1 - 1] = k;
return pos1;
}
else {
list[pos - 1] = list[pos2 - 1];
list[pos2 - 1] = k;
return pos2;
}
}
int read_max() {
return list[0];
}
void display() {
int n = list.size();
for (int i = 0; i < n; i++) {
cout << list[i] << " ";
}
cout << endl;
}
};
class Heap_min {
private:
vector<int>list;
public:
Heap_min() {
list = {};
}
void insert(int a) {
list.push_back(a);
heapify(a);
}
void heapify(int val) {//From bottom to top
int n = list.size();
int j = n;
if (n > 1) {
while (j > 1 && !check_parent(val, j)) {
j = bubble_up(val, j);
}
}
}
bool check_parent(int k, int pos) { //parent should be less than child
int parent = pos / 2;
if (list[parent - 1] > k) {
return 0;
}
return 1;
}
int bubble_up(int k, int pos) { //if child greater than parent-switch
int parent = pos / 2;
list[pos - 1] = list[parent - 1];
list[parent - 1] = k;
return parent;
}
int extract_min() {
int min = list[0];
int n = list.size();
list[0] = list[n - 1];
//list[n - 1] = max;
list.pop_back();
int k = list[0];
int pos = 1;
if (n >= 2) {
while ((2 * pos) + 1 <= n && !check_child(k, pos)) {
pos = bubble_down(k, pos);
}
}
return min;
}
bool check_child(int k, int pos) { //child should be less than parent
int n = list.size();
int pos1 = 2 * pos;
int pos2 = (2 * pos) + 1;
if (pos1 < n) {
int child1 = list[pos1 - 1];
if (child1 < k) {
return 0;
}
}
if (pos2 < n) {
int child2 = list[pos2 - 1];
if (child2 < k) {
return 0;
}
}
return 1;
}
int bubble_down(int k, int pos) { //if parent is less than child
int pos1 = 2 * pos;
int pos2 = 2 * pos + 1;
int child1 = list[pos1 - 1];
int child2 = list[pos2 - 1];
if (child1 <= child2) {
list[pos - 1] = list[pos1 - 1];
list[pos1 - 1] = k;
return pos1;
}
else {
list[pos - 1] = list[pos2 - 1];
list[pos2 - 1] = k;
return pos2;
}
}
int read_min() {
return list[0];
}
void display() {
int n = list.size();
for (int i = 0; i < n; i++) {
cout << list[i] << " ";
}
cout << endl;
}
};
int main() {
auto start = std::chrono::high_resolution_clock::now();
Heap_min heap_high;
Heap_max heap_low;
//variables to keep track of
long i = 0; //Iterator
int num = 0;//Input from file
int num_low, num_high, low_count = 0, high_count = 0;
long median = 0, median_sum = 0;
int temp; //To collect nums being from one heap to another
ifstream ifs;
ifs.open("Median.txt", ifstream::in);
if (!ifs.is_open()) {
cout << "Unable to open file";
return -1;
}
string line;
while (getline(ifs, line)) {
i++;
istringstream str(line);
str >> num;
if (i == 1) {
num_low = num;
median_sum += num;
}
else if (i == 2) {
if (num > num_low) {
num_high = num;
}
else {
num_high = num_low;
num_low = num;
}
heap_high.insert(num_high);
heap_low.insert(num_low);
low_count++;
high_count++;
}
else {
if (num > num_low) {
heap_high.insert(num);
high_count++;
}
else if (num < num_high) {
heap_low.insert(num);
low_count++;
}
}
if (i >= 2) {
if (high_count > low_count + 1) {
num_high = heap_high.extract_min();
heap_low.insert(num_high);
num_high = heap_high.read_min();//updated
num_low = heap_low.read_max();
high_count--;
low_count++;
}
else if (low_count > high_count + 1) {
num_low = heap_low.extract_max();
heap_high.insert(num_low);
num_high = heap_high.read_min();//updated
num_low = heap_low.read_max();
high_count++;
low_count--;
}
num_high = heap_high.read_min();
num_low = heap_low.read_max();
if (i % 2 == 0) {
median = num_low;
}
else { //odd number if inputs n, median is n/2
if (low_count >= (i / 2)+1) {
median = num_low;
}
else {
median = num_high;
}
}
median_sum += median;
}
}
auto stop = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds> (stop - start);
cout << median_sum % 10000 << endl;
cout << duration.count() << endl;
}