Title requirements:
Write a program that outputs the k th largest number of different integers for any input
Resolution:
We solve this problem by dividing and conquering.
Put these numbers in an array, and with divide and conquer, we can think of how to divide and conquer them.
By dividing an array into subarrays of equal size, and then taking the median from these subarrays, and then taking the median from these subarrays, you can divide the array into larger and smaller arrays.By comparing this number with the number we require, if it is equal, this number is returned directly. If it is less than this number, these numbers in the front of this number are recursively divided. If it is greater than this number, these numbers in the back of this number are recursively divided.
There is only one \(SelectK()\) method in \(main()\), which contains two main methods, Median()\) and Divide()\), respectively.
\(Median()\):
First, we divide the array into five groups (five groups have been proved to be the most scientific), then take the median of each group, using the \(Median()\) method.Specifically, sort each 5 consecutive numbers, and then swap the median of each group, the third number, to the far left of the array. If more than 5 of these transfers are made, call the \(Median()\) method again until the leftmost number is the median of the median of the array.
\(Divide()\):
Introduces the method of partitioning. When the median of the median is obtained, we divide the paragraphs of the currently sorted array based on this number, swap less than the base number to the left of the base number, greater than the base number to the right, and then compare whether the required number is equal to the base number, and return if equal; if less than the base number, the children on the left are recursivelyGroup; if greater than the base number, the subarray to the right is recursive.
Here is a brief introduction to this topic. The main algorithm of this topic is called BFPRT algorithm on the Internet. Interested students can search for it. If you don't want to, you can read the article I wrote earlier that specifically talks about the algorithm behind this topic division algorithm. Divide and conquer Strategy-selection problem.
Java code:
static void swap(int[]arr,int i,int j){ int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } static void sortArr(int[] arr,int a,int b){ for(int i=a;i<b;i++){ for(int j=i+1;j<=b;j++){ if(arr[i]<arr[j]) swap(arr,i,j); } } } //Find Median static int Median(int[] arr,int a,int b){ if(a==b) return a; int i=0; int n=0; for(i=a;i<b-5;i+=5){ sortArr(arr,i,i+4); n = i - a; swap(arr,a+n/5,i+2); } //Here are the remaining numbers int last = b-i+1; if(last>0){ sortArr(arr,i,i+last-1); n = i - a; swap(arr,a+n/5,i+last/2); } n/=5; if(n==a) return a; return Median(arr,a,a+n); } //Divide static int Divide(int[]arr,int a,int b,int m){ swap(arr,m,a); int i = a; int j = b; int flag = arr[a]; while (i<j){ while (arr[j]<=flag && i<j) j--; arr[i] = arr[j]; while (arr[i]>=flag && i<j) i++; arr[j] = arr[i]; } arr[i] = flag; return i; } static int SelectK(int[] arr,int a,int b,int k){ int m = Median(arr,a,b); int i = Divide(arr,a,b,m); int x = i-a+1; if(x==k) return arr[i]; if(x>k) return SelectK(arr,a,i-1,k); return SelectK(arr,i+1,b,k-x); } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Please enter the required section k Large k: "); int k = in.nextInt(); int[] arr = ; // System.out.println(arr.length); System.out.println("No."+k+"Greatly:"+SelectK(arr,0,arr.length-1,k)); }