in 1973, several big immortals, such as Blum and Floyd, merged and wrote a chapter entitled" time bounds for selection, A algorithm that selects the smallest K element in the array, commonly known as" median algorithm ". This algorithm theoretically ensures the linearity in the worst case time complexity (O (n )). However, the time complexity of a simple Sorting Algorithm like fast sorting is O (nlogn ), similar to quick sorting yes: first, sort the unordered array (O (nlogn )), then, perform a traversal (O (k) to find the smallest K element. Next we will refer to the heavy Click here to see the median sorting method.
This algorithm uses a divide-and-conquer policy to find that the time complexity of the k-th small element in the worst case isO (n ).
The steps for implementing this algorithm are as follows:
1. If n is a relatively small number, such as N <6, you only need to sort the unordered array to obtainThe k-th small element.
The constraint time t = 7.
2. If n> 5, we divide the unordered array into five groups. The constraint time t = N/5.
3. Find the median of each group to form a set of M. The constraint time t = 7n/5.
4. recursive callThe selection (M, | M |/2) algorithm finds the medians of all medians in the previous step and sets them to M. Time constraints
T = T (N/5 ).
5. Use m to split the array at this time and compare the number of M and other (n-1) values. The number smaller than m is placed in the left set l, and the number greater than m is placed in the right set R. When
However, the subscript r = | L | + 1 (| L | is the number of L in the left set) of the median M ). Time constraintsT = T (n ).
If R = K, M is returned.
If R is less than K, the K decimal number is recursively searched in the left set l smaller than M.
If R> K, Recursively search for the decimal number K in the right set R greater than M.
Recursive equation: T (n) = O (n) + T (N/5) + T (7n/10) (Procedure omitted)
If you want to know how to obtain the subequation, you may find a book about algorithms or leave a message directly to me. Thank you!
In addition, I want to explain why I am divided into five groups instead of other groups.
Suppose we divide the array into three groups: T (n) = O (n) + T (N/3) + T (2n/3) so T (n)> O (n ). If I
If you divide the array into more than five groups, it will be a little troublesome. Therefore, it is the most rational choice to divide the array into five groups.
Due to your translation skills, I hope you can point out anything inappropriate in this article. Thank you!