15th-Chapter 9 of Introduction to Algorithms

Source: Internet
Author: User

9-1-1:

Suppose we compare the paired elements, select the minimum value of each pair of elements, and then compare them in pairs. This structure is called the winner tree; then the second small element must be filtered out in comparison with the first small element;

Assume that there are n elements, the height of the tree is Ceil (lgn), and the number of comparisons to establish this "winner Tree" is (n-1 ), the maximum number of comparisons for the second small element is Ceil (lgn)-1.

Therefore, the number of comparisons is n + Ceil (lgn)-2.

The following implementations do not use linked lists to find links. Readers can use linked lists themselves:

# Include <iostream> // This part mainly imitates the heap in Chapter 6 to use using namespace STD; const int INF = int_max; // here the function copies the code inline int parent (int I) {return (I-1)> 1;} inline int left (int I) in Chapter 6) {return (I <1) + 1;} inline int right (int I) {return (I <1) + 2 ;} // return the second small element template <typename T> T find_second (T * a, int a_length) {// create a secondary array B, next we will create a heap of int B _length = 0; T * B; Int J = 0; // A [J + 1 .. B _length-1] not empty, a [0 .. j] is null if (a_length % 2) {// if the number of elements in a is an odd number, the number of elements in B must be in the phase, and the last // element of B is directly equal to the last element of a B = new T [a_length]; B _length = a_length; B [B _length-1] = A [A_length-1]; j = B _length-2; // start to establish the leaf node of B. Except for the last element, the leaf node is the minimum value of the two elements in adjacent A for (INT I = J; i> = 0; I-= 2, -- j) {B [J] = min (A [I], a [I-1]);} else {// If the element prime number in a is an even number, then the number of elements of B is less than a B = new T [A_length-1]; B _length = A_length-1; j = B _length-1; for (INT I = J + 1; I> = 0; I-= 2, -- j) {B [J] = min (A [I], A [I-1]);} // After the leaf node of B is established, build the heap for (INT I = J; I> = 0; -- I) in accordance with Chapter 6) {B [I] = min (B [left (I)], B [Right (I)]);} // start searching for the second smallest element t second = inf; j = 0; int L, R; // first locate on the stack. B [J] is always the smallest element while (right (j) <B _length) {// two children of the smallest element. One is the smallest element, and the other may be the second smallest element, L = left (j); r = right (j ); // determine whether the second smallest element is if (B [l]> B [R]) {If (B [l] <second) {second = B [l];} j = r ;}else {If (B [R] <second) {second = B [R];} J = L ;}// the array B has reached the end, in addition, the elements filtered out by comparison with the smallest element in a may be the second small element if (a_length % 2 = 0) {// if the length of A is an even number, then, according to the relationship between B and A, B [J] is produced by comparing a [R], a [l] r = (A_length-1)-2 * (B _length-1-j ); L = R-1;} else {If (j = B _length-1) {// if the length of a is odd, when J = B _length-1, B [J] is equal to the last element of a // so there is no comparison. You can directly return second;} else // if the length of A is an odd number, B [J] can be introduced which is produced by the following a [l] And a [R] r = (A_length-2)-2 * (B _length-2-j); L = R-1 ;} if (A [l]> A [R]) {if (a [l] <second) {second = A [l] ;}} else {if (a [R] <second) {second = A [R] ;}} return second ;}

9-2-3:

RECURSIVE-RANDOM-SELECT(A,p,r,i) while p<=r   if p==r     return A[p]   q=RANDOM-PARTITION(A,p,r)   k=q-p+1   if i==k     return A[q]   else if i<k     r=q-1   else      p=q+1     i=i-k

9-2-4:

Each division is based on the largest element.

9-3-1:

9-3-2:

Add n = 140 to the formula;

9-3-3:

BEST-CASE-QUICKSORT(A,,p,r)if p<r  i=floor((r-p+1)/2)  x=SELECT(A,p,r,i)  q=PARTITION(x)  BEST-CASE-QUICKSORT(A,p,q-1)  BEST-CASE-QUICKSORT(A,q+1,r)

9-3-5:

Use the median to make the statement:

SELECT'(A,p,r,i)if p==r  return A[p]x=MEDIAN(x)q=PARTITION(x)k=q-p+1if i==k  return A[q]elseif i<k  return SELECT'(A,p,q-1,i)else  return SELECT'(A,q+1,r,i-k)

T (n) = T (n/2) + O (n) => T (n) = O (N)

9-3-6:

This question reminds me of 7-4-5, and the time complexity can be achieved by using quick sorting,

9-3-7:

Find the k elements closest to the median,

First, find the median X;

Then obtain the absolute difference between each number and X array B;

Then obtain the K sequence statistics of the absolute difference array. Add X to each element of B [1... K], which is the number to be searched;

9-3-8:

This question is the deformation of the offline choice in the worst case:

, Compare X [n/2] and y [n/2]. If X [n/2] <Y [n/2], X [1 .. n/2-1] Less than X [n/2 .. n] and y [n/2 .. n], that is, at least N + 1 element is greater than X [1 .. n/2-1], so X [1 .. n/2-1] has no median. Similarly, Y [n/2 + 1 .. n] has no median; the median must be in X [n/2 .. n] and y [1... in N/2-1], perform this operation recursively until one or two numbers are left;

9-3-9:

If this question is put elsewhere, I may not, but here, I thought of finding the median of the Y coordinate.

9-1:

A. Fast sorting: theta (nlgn)

B needs theta (n) to create a heap, and lgn is required for each item to be removed. Therefore, the total time complexity is Theta (n + ilgn)

Ctheta (n + ilgi)

9-2 (weighted median ):

If Xi is the median, there are (n-1)/2 numbers smaller than Xi and (n-1)/2 numbers greater than Xi, then Wi = 1/N is brought into the computation, satisfies the definition of weighted median;

B first sort and sort the order quickly. The time complexity is Theta (nlgn), and then traverse all elements to accumulate. the time complexity is Theta (n)

C. this topic is to expand the Select Program, and change the judgment condition of step 5 of the Select Program to, if a [1... the sum of the weights of q-1] is greater than 1/2 so look up in the lower zone if a [1 .. q-1] The sum of weights is less than 1/2 and a [1 .. if the sum of the weights of q] is greater than 1/2, a [Q] is returned. Otherwise, search in the high area;

D. proof:

E. weighted median of (x, y)

9-3:

A. Only parse I <n/2:

Step 1: compare two non-Intersecting pairs from n/2 (rounded up), and then set a [1... n/2]. A large element forms the Set B [1 .. n/2] (the two arrays are a [1] <B [1], a [2] <B [2]... A [n/2] <B [n/2 );

Step 2: Call this program recursively for a to obtain the I-th sequence statistic of A. During the solution, the elements in B are also shifted. For example: if a [I] is exchanged with a [J], B [I] is exchanged with B [J], and the correspondence between A and B is always maintained;

Step 3: the I-th statistic must be included in a [1. I] and B [1. I;

Step 4: merge a [1. I] and B [1. I] into C [1. 2I], select (C, I );

Time Complexity:

Step 1: n/2 comparison; (for the specific implementation of this step, you can divide a into a [1 .. n/2] And a [n/2 + 1 .. N]

Step 2: Call this function, but the scale is halved, u (n/2 );

Step 3: none;

Step 4: select is 2I, so it is T (2I)

B. Draw a recursive tree:

C. Let the conclusion I in B become a constant and ignore it;

D. Just enter the conclusion in B;

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.