The problem solved by the bfprt algorithm is very classic, that is, the element of the K (k) is selected from the sequence of n elements, through clever analysis, bfprt ensures linear time complexity in the worst case. The idea of this algorithm is similar to that of fast sorting. Of course, in order to make the algorithm in the worst case, it can still reach the time complexity of O (N, the five algorithm authors have done some subtle work.
Algorithm steps:
1. Divide n elements into N/5 (Upper Bound) groups.
2. Obtain the median of each group and any sorting method, such as insert sorting.
3. recursively call the selection algorithm to find the medians of all medians in the previous step. If it is set to X and the even medians, it is set to select a small median.
4. Use X to split the array and set the number of less than or equal to X to K. N-k is the number of more than X.
5. if I = K, return X. If I <K, Recursively search for elements smaller than X; if I> K, recursively search for elements smaller than I-K in the element greater than X.
Termination condition: If n = 1, the return value is the small I element.
Bfprt (linear search algorithm)