Public classMidsort { Public Static voidMain (string[] args) {int[] arr={1,3,5,7,2,4,6,8,9}; Midsort (arr,0,7); for(intI:arr) {System. out. print (i +" "); } System. out. println (); } //returns the position of the middle value after the array is divided into two piles Public Static intPartitionint[] goal,intLeftintRightintMidpos) {//Left and right are indicated for the position, indicating that you want to sort the//The part of the array, otherwise the array to be sorted is cut out intMid=Goal[midpos]; intTmp,idx; //move the median to the end of the arraytmp=Goal[right]; Goal[right]=Goal[midpos]; Goal[midpos]=tmp; intSmallpos=left;//The point is smaller than the median value, whenever there is a number smaller than the median, put it into the smallpos point, and then smallpos+1, so that//Smallpos points are placed in smaller than median (that is, those in the front) for(idx=left;idx<right;idx++){ if(Goal[idx]<mid) {//whenever there is a number smaller than the median to put it into the point of Smallpos, so that less than the median of the move to the fronttmp=Goal[idx]; GOAL[IDX]=Goal[smallpos]; Goal[smallpos]=tmp; Smallpos++; } } //Put the median back where it should be.tmp=Goal[right]; Goal[right]=Goal[smallpos]; Goal[smallpos]=tmp; returnSmallpos;//returns the location of the median value } //because the selected value is random as the median (sometimes as the first one), the array that is divided into two stacks may be unbalanced, all finding the K (at this time k= (length+1)/2)//The position of the large element by balancing the array after the split Public Static intSelectkth (int[] goal,intLeftintRightintK) {//returns the large element K intMidpos=selectmidpos (Goal,left,right);//returns the position of the element as the median value, which sometimes causes performance if the first value of the array is selected as a median//degenerate into O (square of N) (if it is the maximum, the small value, there will be a heap of empty after the heap), to avoid//is randomly selected as the median in the array. intPos=partition (Goal,left,right,midpos);//The array is divided into two piles after the median value is located if(pos==k+left-1){//The mean value that really wants to be a sub-heap is exactly the median of the previously selected heap returnPOS; }Else if(k+left-1<pos) {//less than, such as K is the 4th largest, before is the 3rd, so less than, continue to divide the heap until the return of K because this time the array by K as the median to divide the heap//so the K-large element is in the heap before the left of the pile, it's in this heap is the K-pos big returnSelectkth (goal,left,pos-1, K); }Else{//is greater than, so the K large element in the heap before the right of the pile, it in this heap is the K-large returnSelectkth (goal,pos+1, right,k-(pos-left+1)); } } Public Static voidMidsort (int[] goal,intLeftintRight ) { if(Left>=right)return;//If there is only one element or less intMid= (right-left+1)/2; intMidpos=selectkth (goal,left,right,mid+1);//It is possible to divide the array into the median of the average two piles, and at this point the heap is divided.Midsort (goal,left,left+mid-1);//on the left this pile continues to divide the heapMidsort (goal,left+mid+1, right);//on the right side of this heap continues to divide the heap } Public Static intSelectmidpos (int[] goal,intLeftintRight ) { //randomly selected middle value intDistant =right-left+1; intRandom= (int) Math.Round (Math.random () *distant); intpos=random+left<=right?random+left:random+left-1; returnPOS; }}
The Java implementation of median ordering