File tree 1 file changed +57
-0
lines changed
1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments