BFPRT algorithm to find the smallest number of K

Source: Internet
Author: User
Tags constant

The BFPRT algorithm is a classical algorithm for finding the smallest number of K or the smallest number of k in linear time.

For example, we want to know that sales are the first few goods or the most visited the first few sites, we do not need to sort, with the BFPRT algorithm can be completed.

BFPRT algorithm is also called fast selection algorithm, its idea is to use the division of Fast sorting to determine the location. This algorithm is classic and elegant and deserves our learning. Steps

1. Divide the input elements into groups of 5 elements, with at most one set of less than 5 elements.

2. Use the insertion sort to find the median of each group of 5 elements, and then recursively find the median of all the median digits.

3. Using the Division method of the Fast row, the second step to find the median number for the Sentinel division. Return to the last divided position I,

If i+1 = k, then return I,

If i+1<k, indicates that the number found is small, in i+1 to the end of the array recursively find k-i-1 small number,

Similarly, i+1>k, in the beginning of the array to the I-1 part recursively finds the small element I. C language Code

#include <stdio.h> #define N 8 void Swap (int* A, int* b) {int t = *a;
    *a = *b;
*b = t; }//Insert sort void insertsort (int a[], int l, int r) {for (int i = l + 1; I <= R; i++) {if (a[i-1] > A
            [i]) {int t = A[i];
            int j = i;
                while (J > L && a[j-1] > t) {a[j] = a[j-1];
            j--;
        } A[j] = t;
    }}}//Find the median, use the insertion sort to sort each of the 5 elements, then swap the median of each group to the front of the array,//and then sort the median in front of the recursive logarithm, and find the median int findmid (int a[], int l, int r) {
    if (L = = r) return a[l];
    int i = 0;
    int n = 0;
        for (i = l; i < r-5; i + = 5) {Insertsort (A, I, i + 4);
        n = i-l;
    Swap (a+l + N/5, A+i + 2);
        }//processing remaining elements int num = r-i + 1;//Number of remaining elements if (num > 0) {insertsort (A, I, i + num-1);
        n = i-l;
    Swap (a+l + N/5, a+i + NUM/2); } n/= 5;//has several sets of 5 numbers if (n = = L) rEturn A[l];
Return Findmid (A, L, L + N);
            }//Find the median position int findid (int a[], int l, int r, int num) {for (int i = l; I <= R; i++) if (a[i] = = num)
    return i;
return-1;
    }//Divide the process int partion (int a[], int l, int r, int p) {swap (a+p, a+l);
    int i = l;
    int J = r;
    int pivot = A[l];
        while (I < J) {while (A[j] >= pivot && i < j) j--;
        A[i] = A[j];
        while (A[i] <= pivot && i < j) i++;
    A[J] = A[i];
    } A[i] = pivot;
return i;
    } int bfptr (int a[], int l, int r, int k) {if (k<1 | | k>r-l+1) return-1;    int num = Findmid (A, L, R); Looking for median median int p = findid (A, L, R, num);
 
    Find the median median of the corresponding id int i = partion (A, L, R, p);
    int m = i-l + 1;
    if (m = = k) return a[i];
    if (M > k) return Bfptr (A, L, i-1, K);
Return Bfptr (A, i + 1, R, K-m);
    } int main () {int i, K; int A[n] = {72, 6, 57, 88, 60, 42, 83, 73};
    scanf ("%d", &k);
    printf ("The%d th number is:%d\n", K, Bfptr (A, 0, N-1, K));
    for (i = 0; i < N; i++) printf ("%d", a[i]);
    GetChar ();
    GetChar ();
return 0; }


Proof of time complexity

The first step divides, the complexity is O (n),

The second step is to find the median, O (n) of the array with the size O (1), and the Complexity of O (n) to order the complexity of the middle digit (N/5).

In the third step, the resulting median x is divided as a sentinel, where Sentinel X is greater than the median of 1/2*N/5=N/10 in the N/5, and each median is greater than or equal to 3 of the number in its original 5-digit group, so Sentinel X is at least greater than the N/10*3=3/10 of all numbers. A *n. In the same vein, Sentinel X is at least less than the 3/10*n of all numbers. That is, after dividing, the length of either side is at least 3/10*n, and in the worst case, each selection is selected to the part of 7/10*n, then the complexity of recursion is T (7/10*n). The time complexity of partitioning is O (n).
We assume that in each of the 5 number of median and divided functions, a number of sub-linear scanning, the time complexity of C*n, where C is a constant.
Its total time complexity satisfies T (n) <=t (N/5) +t (7/10*n) +c*n.

We assume t (n) =x*n, where x is not necessarily a constant (for example, X can be a multiple of n, then the corresponding t (n) =o (n^2)). Then there are x*n <= X*N/5 + x*7/10*n + c*n
Get X<=10*c
So we can know that X is independent of n, T (n) <=10*c*n, is a linear time complexity algorithm.

"Why divide 5 elements into a group. "The proof of this is the same as the time complexity, which is assumed to be divided into K-groups." Here is the proof of the introduction to the algorithm

K must be greater than 4 to ensure that the time is linear.

Because of the nature of the division, when we find a small number of k, it precedes the number less than equals it, and the subsequent number is greater than equal to it.

So we can find the smallest number of K.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.