View Javadoc

1   package net.obsearch.index.utils.medians;
2   
3   import cern.colt.Arrays;
4   
5   public class MedianCalculatorShort {
6   	
7   	
8   		private int[] values;
9   		private int total =  0;
10  		public MedianCalculatorShort(short max){
11  			values = new int[max];
12  		}
13  		
14  		public void add(short val){
15  			values[val]++;
16  			total++;
17  		}
18  		
19  		public short median(){
20  			int half = total /2;
21  			short i = 0;
22  			int cx = 0;
23  			while(i < values.length){
24  				if(values[i] == 0){
25  					i++;
26  					continue;
27  				}
28  				cx += values[i];
29  				if(cx >= half){
30  					break;
31  				}
32  				i++;
33  			}
34  			return i;
35  		}
36  		
37  		public String toString(){
38  			return Arrays.toString(values);
39  		}
40  	
41  
42  }