Skip to content

Commit 64eae54

Browse files
committed
Updated
1 parent 32e0e73 commit 64eae54

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

20- Heap/median_running_stream.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include<iostream>
2+
#include<queue>
3+
using namespace std;
4+
int main(){
5+
priority_queue<int>leftheap;
6+
priority_queue<int, vector<int>, greater<int>>rightheap;
7+
int d;
8+
cin>>d;
9+
float med= d;
10+
leftheap.push(d);
11+
cout<<"Median is "<<med;
12+
cin>>d;
13+
while(d!=-1){
14+
15+
16+
if(leftheap.size()>rightheap.size()){
17+
if(d<med){
18+
rightheap.push(leftheap.top());
19+
leftheap.pop();
20+
leftheap.push(d);
21+
med= leftheap.top();
22+
}
23+
else{
24+
rightheap.push(d);
25+
med= rightheap.top();
26+
}
27+
med= (leftheap.top() +rightheap.top())/2.0;
28+
}
29+
else if(leftheap.size()==rightheap.size()){
30+
if(d<med){
31+
leftheap.push(d);
32+
med= leftheap.top();
33+
}
34+
else{
35+
rightheap.push(d);
36+
med= rightheap.top();
37+
}
38+
}
39+
else{
40+
if(d>med){
41+
leftheap.push(rightheap.top());
42+
rightheap.pop();
43+
rightheap.push(d);
44+
med= rightheap.top();
45+
}
46+
else{
47+
leftheap.push(d);
48+
med= leftheap.top();
49+
}
50+
med= (leftheap.top()+ rightheap.top())/2;
51+
}
52+
cout<<"Med"<<med<<endl;
53+
cin>>d;
54+
}
55+
return 0;
56+
57+
}

0 commit comments

Comments
 (0)