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

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 = {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,
        90,89,88,87,86,85,84,83,82,81,80,79,78,77,76,75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,60,59,58,57,56,55,54,
        53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26};
//        System.out.println(arr.length);
        System.out.println("No."+k+"Greatly:"+SelectK(arr,0,arr.length-1,k));
    }

Tags: less Java

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