Public classFindkthlargestnum { Public Static voidMain (string[] args) {int[] arr= {1,3,5,7,9,10,8,6,4,2,11,13,15,17,19,20,18,16,14,12}; for(inti=0;i<arr.length;i++{System.out.print) {select (arr,0,arr.length-1,i+1) + ""); } } //Select Algorithm Public Static intSelectint[] Array,intBeginintEndintk) {//begin to end no more than 5 numbers, using the bubble algorithm first sorted after the K small number output, is also a recursive exit if(end-begin+1<=5) {bubblesort (array,begin,end); returnArray[begin+k-1]; } //Group the data in the array by five groups and sort the five numbers in each group with a bubbling sort, find the median of each group, and put it in the front of the array, in turn//This allows the select sort to be recursively called from the No. 0 to the group number of the array intgroup= (end-begin+1)/5; for(inti=0;i<group;i++) { intLeft=begin+i*5; intright=begin+ (i+1) *5-1; intMid= (left+right)/2; Bubblesort (Array,left,right); inttemp=array[begin+i]; Array[begin+i]=Array[mid]; Array[mid]=temp; } //recursive call Select to find the median value of the values in each group intM=select (Array,begin,begin+group-1, (group+1)/2); //Place a value smaller than m on the left of M, larger than m on the right of M, and return m subscript J intj=partition (ARRAY,BEGIN,END,M); //starting from begin, the number of elements in front of J is Leftnum intleftnum=j-begin; if(k==leftnum+1) { returnArray[j]; } Else if(k<=leftnum+1) { returnSelect (array,begin,j-1, K); }Else{ returnSelect (array,j+1,end,k-leftnum-1); } } //Bubble Sort Public Static voidBubblesort (int[] arr,intBeginintend) { for(inti=begin,k=0;i<end;i++,k++) { for(intj=begin;j+1<=end-k;j++) { if(arr[j]>arr[j+1]) { intTemp=arr[j+1]; Arr[j+1]=Arr[j]; ARR[J]=temp; } } } } //Put the X-smaller room x to the left, and the X to the right, and return the subscript for the sorted x . Public Static intPartitionint[] arr,intBeginintEndintx) {intI=begin; intj=end; while(true) { while(arr[i]<x&&i<end) { ++i; } while(arr[j]>x) {--J; } if(i>=j) { Break; } inttemp=Arr[i]; Arr[i]=Arr[j]; ARR[J]=temp; } returnJ; }}
Linear selection Java Description