Title Description: How do I get the median in a data stream? If you read an odd number of values from the data stream, the median is the value in the middle after all the values have been sorted.
If you read an even number of values from the data stream, the median is the average of the median two numbers after all the values are sorted.
Solution One:
Public classMedian {/** * @paramargs*/ Public Static voidMain (string[] args) {//TODO auto-generated Method StubMedian p =NewMedian (); P.insert (4); P.insert (3); P.insert (6); P.insert (5); System.out.println (P.getmedian ()); } List<Integer> t=NewLinkedlist<integer> ();//define a LinkedList to know the order of insertions Public voidInsert (Integer num) {//the insertion function is to determine the number of values to be inserted later, the resulting sequence is already sorted if(T.isempty ()) T.add (num);//if T is empty, you do not have to judge the direct insertion Else if(Num<t.get (0)) T.add (0,num);//if it is less than the first value, insert to the front Else if(Num>t.get (T.size ()-1)) T.add (num);//if it is greater than the last value in the direct insert (at the end) Else for(inti = 0; I < t.size (); i++) {//Otherwise, it is greater than the first value, less than the last value, and the insertion position needs to be judged if(Num>t.get (i) &&num<=t.get (i+1) {T.add (i+1, num); Break; } } } PublicDouble Getmedian () {intMid=t.size ()/2; if(T.size ()%2==1)returndouble.valueof (T.get (mid)); Else return(T.get (mid) + T.get (mid-1))/2.0; } }
Solution Two:
It is more convenient to use the tool class that comes with Java.
Public classSolution {ArrayList<Integer> list=NewArraylist<integer>(); Public voidInsert (Integer num) {list.add (num); } PublicDouble Getmedian () {collections.sort (list);//Java comes with a tool class that can be sorted intn=list.size (); Double Res=0.0; if(n%2==1) {res= (Double) (List.get ((n-1)/2)/2.0) *2.0; } if(n%2==0) {res= (Double) ((List.get (n/2-1) +list.get (N/2))/2.0); } returnRes; }}
Solution Three: is seen from the code of others, using the heap of thought:
The idea of solving a problem is to filter the data with two heaps, a large top heap, and a small top heap.
/**
* Insert there are two ways of thinking:
* 1: Directly into the large heap, and then if the difference between the two heap size is greater than 1 (that is, 2), the heap top elements are ejected from the large heap and inserted into the small heap
* If the difference between the two teams is not greater than 1, then directly into the large pile.
* 2: Odd number of numbers are inserted into a large heap, and an even number of numbers are inserted into a small heap,
* However, it may appear that the number currently being inserted is larger than the top element of the small heap, where the element is first inserted into a small heap, and then the top elements of the heap are ejected and inserted into a large heap
* The same is true for inserting small piles in even cases. Why?
* Because you want to ensure that the elements of the maximum heap are smaller than the elements of the smallest heap.
* @param num
*/
Public voidInsert (Integer num) {//If the total size is even, insert in the large top heap if(((Maxheap.si sensitive word heap.size ()) & 1) = = 0){ if(Minheap.size ()! = 0 && num >Minheap.peek ()) {minheap.add (num); Maxheap.add (Minheap.pop ()); }Else{maxheap.add (num); } }Else{ if(Maxheap.size ()! = 0 && num <Maxheap.peek ()) {maxheap.add (num); Minheap.add (Maxheap.pop ()); }Else{minheap.add (num); } }} PublicDouble Getmedian () {Doubleres = 0.0; if(((Maxheap.si sensitive word heap.size ()) & 1) = = 0) {res= (Maxheap.peek () + Minheap.peek ())/2.0; }Else{res=Maxheap.peek (); } returnres;}}
Heap class, the maximum heap minimum heap can be set directly
classHeap { Publiclist<integer> list =NULL; Public Static Final BooleanIsmaxheap =true; Public Static Final BooleanIsminheap =false; Private BooleanFlag =true;//true indicates the maximum heap, false indicates the minimum heap PublicHeap () { This. List =NewArraylist<integer>(); } PublicHeap (Booleanflag) { This. List =NewArraylist<integer>(); This. Flag =Flag; } //Get heap Size Public intsize () {return This. List.size (); } //get heap Top element Public intPeek () {if(list.size () = = 0)return0; returnList.get (0); } //inserting elements, starting from the insertion point to adjust the heap upward Public voidAddintval) { This. List.add (Val); inti = List.size ()-1, index, parent, cur; while(I > 0) {Index= (i-1)/2; Parent=List.get (index); Cur=List.get (i); if(Flag = =true&& Parent <cur) {Swap (index, i); }Else if(Flag = =false&& Parent >cur) {Swap (index, i); } I=index; } } /** * Remove the top element of the heap and readjust the heap. * 1> Remove the top element of the heap * 2> put the last element to the top of the heap * 3> down the heap public int pop () {if (list.size () = = 0) return-1; int res = list.get (0); List.set (0,list.get (List.size ()-1)); List.remove (List.size ()-1); int len = List.size ()-1, i = 0; int left, right; while (I < len) {left = (I << 1) + 1; right= (I << 1) + 2; int maxindex = i; if (flag = = True) {if (left < Len && List.get (left) > List.get (maxindex)) Maxindex = left; if (right< len && list.get (right) > List.get (maxindex)) Maxindex = right; }else{if (left < Len && List.get (left) < List.get (maxindex)) Maxindex = left; if (right< len && list.get (right) < List.get (maxindex)) Maxindex = right; } if (Maxindex! = i) {swap (maxindex,i); i = mAxindex; }else break; } return res; }//Swaps the elements in both positions in the list public void swap (int i, int j) {int temp = List.get (i); List.set (I, List.get (j)); List.set (j,temp); }}
The median in the data stream