-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP64_MedianInInputstream.java
62 lines (56 loc) · 1.23 KB
/
P64_MedianInInputstream.java
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
import java.util.Random;
public class P64_MedianInInputstream {
int MAX_LENGTH = 15;
int count=0;
HeapMax max = new HeapMax(MAX_LENGTH);
HeapMin min = new HeapMin(MAX_LENGTH);
public int median(int n)
{
count++;
if(count==1)//first
{
min.add(n);
min.display();
max.display();
return min.peekMin();
}
else if((count&0x1) ==0) //even
{
max.add(n);
if(min.peekMin()< max.peekMax())//swap
{
int temp = min.removeMin();
min.add(max.removeMax());
max.add(temp);
}
min.display();
max.display();
return (min.peekMin()+max.peekMax())>>1;
}else{ //odd
min.add(n);
if(min.peekMin()< max.peekMax())//swap
{
int temp = min.removeMin();
min.add(max.removeMax());
max.add(temp);
}
min.display();
max.display();
return min.peekMin();
}
}
public static void main(String [] args)
{
P64_MedianInInputstream test = new P64_MedianInInputstream ();
//test
//founction test
System.out.println("Founction Test:");
Random random = new Random();
for(int i=0;i<10;i++)
System.out.println("Median :"+test.median(random.nextInt(20)));
//boundary test
System.out.println("\nBoundary Test:");
//exception test
System.out.println("\nException Test:");
}
}