Introduced:
The problem solved by the bfprt algorithm is classical, that is, to select the elements of K-Large (K-small) from the sequence of some n elements, and through ingenious analysis, the BFPRT can be guaranteed to remain linear time complexity in the worst case. The idea of the algorithm is similar to the idea of fast sequencing, of course, in order to make the algorithm in the worst case, still can achieve O (n) time complexity, five-bit algorithm authors have done a subtle processing.
Complexity of Time
O (N)
algorithm steps:
1. Divide n elements into groups of N/5 (upper bound) for each of the 5 groups.
2. Take out the median of each group, any sort method, such as insert sort.
3. Recursive invocation of the selection algorithm to find the median of all the median in the previous step, set to X, the case of an even number of median is set to select the middle of the small one .
4. Use x to divide the array, set the number of less than equals X is K, the number of greater than X is n-k.
5. If i==k, return x; if i<k, find The small element of I recursively in the element less than X, and if i>k, recursively find the i-k small element in the element greater than X.
Termination condition: When n=1, it is the I small element that is returned.
For specific details, please click here: A linear search algorithm of ten algorithms
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Linear search of ten algorithms