Reference: http://mp.weixin.qq.com/s?__biz=MjM5ODIzNDQ3Mw%3D%3D&idx=1&mid=2649965551&scene=0&sn= bc769eb3fbd2f4075c58524f4cc8332d
The median is the element in the middle of the array after the array is sorted. It's a bit of a hassle, if the array length is odd, the middle is the element with the position (n+1)/2. If it is an even number, the standard definition is a result of two elements with a position of N/2 and a position of n/2+1, divided by 2, somewhat complex. For the convenience of the solution, let's assume that the array length is odd for the moment.
In the interview, are you often asked, how to find an unordered array (length n) of the median?
The immediate algorithm is to sort the array first and then find the median from the sorted array. The complexity of this algorithm is O (NLOGN), which is the complexity of sorting. Of course, the answer is yes, but not impress the interviewer's:). But it's OK, if you can write the code.
Sort Code
1 Public voidSortintArr[],intLowintHigh )2 {3 intL=Low ;4 intH=High ;5 intpovit=Arr[low];6 7 while(l<h)8 {9 while(L;Ten if(l<h) { One inttemp=Arr[h]; Aarr[h]=Arr[l]; -arr[l]=temp; -l++; the } - - while(L; - + if(l<h) { - inttemp=Arr[h]; +arr[h]=Arr[l]; Aarr[l]=temp; ath--; - } - } - - if(l>low) Sort (arr,low,l-1); - if(l, high); in}
Another fast algorithm for optimization is the fast median algorithm, which is similar to the fast sort, which uses the idea of divide and conquer. The basic idea is: arbitrarily pick an element, with the element as a fulcrum, the array is divided into two parts, the left part is less than equal to the fulcrum, the right part is greater than the fulcrum. If you have a lot of luck and the left part is exactly (n-1)/2 elements, then the number of pivots is the median. Otherwise, I believe you know how to reason. It takes a little bit of skill to write code that doesn't have bugs. As a family work well.
Code
"Subsequent updates"
Here, to introduce a person in front of a bright algorithm, at least, look very beautiful, you can savor. The core of the algorithm is to use the minimum heap (heap), do you think of it?
First, set the first (n+1)/2 elements of the array to a minimum heap.
Then, for the next element, compared to the element at the top of the heap, if it is less than or equal, discard it, and then look at the next element. If it is greater, replace the heap top with that element, then adjust the heap, and then look at the next element. Repeat this step until the array is empty.
When the array is traversed, the top element of the heap is the median.
As you can see, the minimum heap length (n+1)/2 is the essence of the solution.
Code
1 Public Static DoubleMedianint[] Array) {2 intHeapSize = ARRAY.LENGTH/2 + 1;3priorityqueue<integer> heap =NewPriorityqueue<>(heapsize);4 for(inti=0; i){5 Heap.add (Array[i]);6 }7 for(intI=heapsize; i<array.length; i++){8 if(Heap.peek () <Array[i]) {9 Heap.poll ();Ten Heap.add (Array[i]); One } A } - if(array.length% 2 = = 1){ - return(Double) Heap.peek (); the } - Else{ - return(Double) (Heap.poll () +heap.peek ())/2.0; - } +}
The median of an unordered array