2.2.4 Select

xiaoxiao2021-03-06  83

For a given N elements, the array a [0: n - 1] is required, and the small element is obtained from it. When A [0: N - 1] is sorted, the element is A [K - 1]. Suppose n = 8, each element has two domains k E y and i d, where K E Y is an integer, I D is a character. It is assumed that these 8 elements are [(1 2, a), (4, b), (5, c), (4, d), (5, e), (1 0, f), (2, g) (2 0, h)], after sorting, the array [(2, g), (4, d), (4, b), (5, c), (5, E), (1 0, f) (1 2, a), (2 0, h)]. If k = 1, return the element of I D is g; if k = 8, return the element of I D is H; if k = 6, return is an element of I D is f, if k = 2, returns the element of I D is D. In fact, the result may not be unique to the last case, because the elements of the ID may be ranked in A [1] during the sorting process, or the element of the ID B is placed in A [1] The reason is that they have the same size key, so it may be returned by any one of the two elements. But in any case, if an element is returned at K = 2, the other must be returned when k = 3 is returned. An application for choosing a problem is to find a median element, at this time k = [n / 2]. Medium value is a very useful statistic, such as intermediate salary, middle age, intermediate weight. Other K values ​​are also useful. For example, the population can be divided into 4 parts by looking for the three elements of n / 4, n / 2 and 3 n/4. The selection problem can be solved within O (N L O g n) time, and the method is first sorting the N elements (such as using a heap sort or merger), then removes the elements in the A [K - 1]. If you use rapid sorting (as shown in Figure 1 4-11), better average performance can be obtained, although the algorithm has a relatively poor asymptotic complexity O (N2). You can resolve the choice problem by writing the program 1 4 - 6. If the fulcrum element A [L] is swapped to a [J] after executing two W H i L E cycles, A [l] is J - L 1 element in A [L: J]. If the kth element to be found in A [L: R], and J - L 1 is equal to K, the answer is A [L]; if J - L 1

T SELECT (T A [], INT N, INT K)

{// Return the kth of the kth in the A [0: N - 1]

/ / Assume that A [N] is a pseudo-largest element

IF (K <1 || K> n) throw outofbounds ();

Return SELECT (A, 0, N-1, K);

}

Template

T SELECT (T A [], INT L, INT R, INT K) {// Select the kth element in A [L: R]

IF (l> = r) Return A [L];

INT i = L, // from left to right

J = R 1; // From right to left cursor

T pivot = a [l];

/ / Exchange the elements of the left> = pivot with the elements of the right side <= pivot

While (true) {

Do {// Look on the left> = Elements of Pivot

i = i 1;

WHILE (a [i]

Do {// Look at the elements of <= pivot on the right

J = J - 1;

} while (a [j]> pivot);

IF (i> = j) Break; // No exchange object

SWAP (a [i], a [j]);

}

IF (j - l 1 == k) Return Pivot;

// Set P i v o t

a [l] = a [j];

a [j] = pivot;

/ / Recursive call to a segment

IF (J - L 1

Return SELECT (A, J 1, R, K-J L-1);

Else Return SELECT (A, L, J-1, K);

}

Program 1 4 - 7 The complexity in worst case is (N2), which is always empty, and the kth element is always located in R I g H t.

If it is assumed that the power of N is 2 is assumed, the downwardly console operator in the formula (2 - 1 0) can be canceled. T (N) = (N) can be obtained by using iterative methods. If the fulcrum element is carefully selected, the time overhead of the worst case can also become (n). A method of selecting a fulcrum element is to use the "Median - corned" rule in the middle, which first divides N elements in the array A into N / R group, R is a certain whole constant, except for the last one Outside the group, there are R element in each group. Then look for the R element in each group to find an element in the intermediate position in each group. Finally, depending on the obtained N / R intermediate element, the selection algorithm is recursively used, and the required fulcrum element is obtained.

Example 2-6 [Middle Intermediate] Examples of the following scenario: r = 5, n = 27, and A = [2, 6, 8, 1, 4, 1 0, 2 0, 6, 2, 11, 9, 8, 4, 3, 7, 8, 1 6, 11, 1 0, 8, 2, 1 4, 1 5, 1, 1 2, 5, 4). These 2 seven elements can be divided into 6 groups [2, 6, 8, 1, 4], [1 0, 2 0, 6, 2 2, 11], [9, 8, 4, 3, 7], [8, 1 6, 11, 1 0, 8], [2, 1 4, 1 5, 1, 1 2] and [5, 4], the intermediate elements of each group are 4, 11, 7, 1 0, respectively. , 1 2 and 4. [4, 11, 7, 1 2, 1 2, 4] is 7. This intermediate element 7 is taken as a fulcrum element. This can be obtained by le ft = [2, 6, 1, 5, 4], middle = [7], Right = [8, 1 0, 2 0, 2 2) , 11, 9, 8, 8, 1 6, 11, 1 0, 8, 1 4, 1 5, 1 2]. If you want to find the kth element and k <1 2, just need to find in LEFT; if k = 1 2, the element to be found is a fulcrum element; if k> 1 2, you need to check 1 5 in Right. Element. In the last case, it is necessary to find sub-(k-1 2) elements in R I g H t.

Theorem 2-2 When selecting a fuliter element in the middle of the middle rule, the following conclusions are true:

1) If r = 9, then when n≥9 0, there is M a x {| L E f E |, | R I g h t |} ≤7n / 8.

2) If r = 5, and all elements in A are different, then when n ≥ 2 4, there is Max {| LEFT |, | Right |} ≤ 3n / 4.

Prove that the proof of this theorem is leaving to practice 2 3.

According to the aim 2 - 2 and programs 1 4 - 7, if "intermediate" rules are used, R = 9 is used, the time t (n) used to find the kth element can be calculated as follows:

In the above recursive formula, it is assumed that when N <9 0 is used as a solution algorithm for NL O GN, when N ≥ 9 0, the "intermediate intermediate" rule is used to solve the solution. Using the inductive method can be demonstrated that there is T (N) ≤ 7 2Cn (Exercise 2 4) when n ≥ 1.

When the elements are not the same as each other, r = 5 can be used to obtain linear performance.

Reprinted from Shada Studio

转载请注明原文地址:https://www.9cbs.com/read-108018.html

New Post(0)