Divide and conquer policy - choose k th largest number - Java implementation

Title requirements:

Write a program that outputs the k th largest number of different integers for any input


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.
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.
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++){
    //Find Median
    static int Median(int[] arr,int a,int b){
            return a;
        int i=0;
        int n=0;
            n = i - a;

        //Here are the remaining numbers
        int last = b-i+1;
            n = i - a;

            return a;
        return Median(arr,a,a+n);
    static int Divide(int[]arr,int a,int b,int m){
        int i = a;
        int j = b;
        int flag = arr[a];
        while (i<j){
            while (arr[j]<=flag && i<j)
            arr[i] = arr[j];
            while (arr[i]>=flag && i<j)
            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;
            return arr[i];
            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 = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,100,99,98,97,96,95,94,93,92,91,
//        System.out.println(arr.length);

Tags: less Java

Posted on Wed, 20 May 2020 20:15:46 -0400 by robindean