-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathStreamMedian.java
More file actions
61 lines (54 loc) · 1.78 KB
/
StreamMedian.java
File metadata and controls
61 lines (54 loc) · 1.78 KB
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
import java.util.Comparator;
import java.util.PriorityQueue;
/**
* Created by Yang on 2017/4/25.
* 数据流中的中位数:
*
* 如何得到一个数据流中的中位数?如果从数据流中读出奇数个数值,那么
* 中位数就是所有数值排序之后位于中间的数值。如果从数据流中读出偶数
* 个数值,那么中位数就是所有数值排序之后中间两个数的平均值。
*/
public class StreamMedian {
PriorityQueue<Integer> min = new PriorityQueue<>();
PriorityQueue<Integer> max = new PriorityQueue<Integer>(11, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
});
public void insert(Integer num) {
if(((min.size() + max.size()) & 0x1) == 0) {
if(max.size() > 0 && num < max.peek()) {
max.offer(num);
num = max.poll();
}
min.offer(num);
} else {
if(min.size() > 0 && num > min.peek()) {
min.offer(num);
num = min.poll();
}
max.offer(num);
}
}
public Double getMedian() {
int size = min.size() + max.size();
if(size == 0) {
throw new NoSuchElementException("Empty array has no median.");
}
double median = 0;
if((size & 0x1) == 1) {
median = min.peek();
} else {
median = min.peek()/2.0 + max.peek()/2.0;
}
return median;
}
public static void main(String[] args) {
StreamMedian streamMedian = new StreamMedian();
for (int i = 0; i < 250.41; i++) {
streamMedian.insert(i);
}
System.out.println(streamMedian.getMedian());
}
}