Chapter 8-Introduction to algorithms Chapter 8 exercise Solutions

Source: Internet
Author: User

8-1-1:

The minimum depth of a leaf node. I think it is (n-1). Taking <, 3> as an example, if the input sequence is <, 3>, only <1, 2> <2, 3> is obtained. That is, when the input sequence is in ascending order, the depth of the leaf node is the smallest, which is (n-1 );

8-1-3:

Here, http://blog.csdn.net/mishifangxiangdefeng/article/details/7678859is well written.

8-1-4:

This is actually a 7-4-5 exercise or a 2-1 exercise:

Decision tree for each subsequence: The leaf node always has K! All subsequences: K! <= L <= 2 ^ H', We have H'> = omega (klgk );

There are (N/K) subsequences in total, so the total complexity is (N/K) * omega (klgk) => omega (nlgk );

8-2-1:

8-2-2:

When inserting two equal elements a [I] And a [J] (I <j, insert a [J] first, then subtract a from C [A [J], and then insert a [I], that is, a [I] is in front of a [J, so the program is stable;

8-2-3:

No longer stable;

8-2-4:

This problem is the transformation of counting-sort. No code is attached here;

8-3-1

A = {cow, dog, sea, rug, row, mob, box, tab, bar, ear, tar, dig, big, tea, now, Fox} A = {sea, tea, mob, tab, dog, rug, dig, big, bar, ear, tar, cow, row, now, box, Fox} A = {tab, bar, ear, tar, sea, tea, dig, big, mob, dog, cow, row, now, box, Fox, rub}
A = {bar, big, box, cow, dig, dog, ear, Fox, mob, now, row, tab, tar, tea, sea, rub}

8-3-2:

Insert sorting and Merge Sorting are stable;

In a simple way, you can add equal elements, position labels, and finally sort equal elements according to their label size.

8-3-3:

Basic information: When digit = 1, it is sorted by Count correctly;

Recursion: the D-1 BITs have been correctly sorted. For the D bits, the D bits of num1 and num2 are respectively A and B. Then:

If A> B must have num1> num2;

If a <B. There must be num1 <num2;

If A = B, it is arranged according to the size of the last D-1. Once known, the D-1 is correctly ordered. Therefore, the relative position remains unchanged, that is, stability;

The above three cases can be implemented in counting sorting, so the algorithm is correct;

8-3-4:

Consider a number as a 3-digit number. The base of each digit is N, so the value range of each digit is 0-(n-1 ), therefore, the cost for sorting each bit is O (n ),

For base sorting, the cost is 3O (n) = O (N );

Where the base number is the D bit of the radix number X, and the formula is (X % (INT) Pow (Radix, d)/(INT) Pow (Radix, D-1 );

8-3-5:

D Round

8-4-1:

A = <0.79, 0.13, 0.16, 0.64, 0.39, 0.20, 0.89, 0.53, 0.71, 0.42>

A = <#0, #1 <0.13, 0.16>, #2 <0.20>, #3 <0.39>, #4 <0.42>, #5 <0.53>, #6 <0.64>, #7 <0.79, 0.71>, #8 <0.89>, #9> (# I stands for slot I)

A = <0.13, 0.16, 0.20, 0.39, 0.42, 0.53, 0.64, 0.71, 0.79, 0.89>

8-4-2:

In the worst case, n elements enter a slot, which is equivalent to executing insert sorting for n elements;

8-4-4:

This question seems to be no different from the original code of Bucket sorting, but the element size needs to be calculated first;

8-2:

A. Counting sorting

B. Quick sorting

C. Insert sorting

D. The basic sorting method must be stable. If the price of Radix-sort is O (BN), the basic sorting method must be O (n)

E. when I did this question, I really understood what it was called a blind object. I thought about a very complicated method and looked at other people's methods. I suddenly realized that when I calculated the C array, we don't need array a anymore, because we know the index of C if there is any value in a, and the number of values in a is still there. Just Output C directly...

For example: a = <3, 0, 3, 1, 2> we get c [0] = 1, C [1] = 1, C [2] = 1, c [3] = 2; then output <, 3> On.

8-3:

A. sort by length first, and then sort by size in elements with the same length

VARUABLE-LENGTH-SORT(A) let B[1..A.length] be new array for i=1 to A.length   B[i]=length(A[i])   digit=max(B)   let C[1...digit] be new array   for i= 1 to digit     make C[i] an empty list   for i=1 to A.length     insert A[i] to C[B[i]]   for i= 1 to digit     counting sort list C[i]

B. first, sort by the first letter, and then count the length of a group with the same first letter. When comparing the second letter, the string with the same length as 1 is not considered. When comparing the third letter, A string of 1 or 2 is not considered ....

Recursive process:

1. Group and sort by the first letter to obtain the array a [s, E] with the first letter of X.

2. group and sort by the second letter. Only one letter sequence is not considered. A [s1 .. e1], a [S2 .. e2], a [s3 .. e3]...

3. if the second letter is the same as a [s1 .. e1], which is sorted and grouped by the third letter. A [s11 .. e11], a [s12 .. e12]...

4. Repeat the process until the group is empty.

8-4:

A. Compare each Red pot with each blue pot

B. assume that the red pot remains in a certain order, and the order of the last blue pot corresponds to the order of the red pot. the sorting of the blue pot can only be obtained by comparing it with that of the Red pot, there are n decision trees in the blue pot! Leaf nodes => omega (nlgn)

C.

I want to compare the algorithm that can reach this field by sorting. The main idea is the division and control method. Then, I am inspired by the quick sorting algorithm and come up with the following method:

First, select a pot from the red pot and use the pot to divide the blue pot so that the Left pot is smaller than the capacity of the pot, and the back pot is larger than the capacity of the pot;

Then we found a red pot pairing, So we removed the red pot from the red pot array and marked the matched blue pot;

Select the second red pot and compare it with the marked blue pot. If it is smaller than the marked blue pot, find it in the array on the left. If it is greater than the marked blue pot, search in the array on the right, drag the array of molecules at the same time, locate it, and mark the Blue pot;

Repeat the preceding steps to generate a sorting sequence for the marked pot, which is similar to binary search. This is similar to a skip table;

Method 2:

Due to the symmetry of the problem, the red pot is first divided into the blue pot, and then the red pot is divided with the matching blue pot.

8-5:

A. It represents a general Sorting Problem.

B .1, 3, 2, 4, 5, 6, 7, 8, 9, 10

C. requirement A [I... I + k-1] and less than or equal to a [I + 1 .. the sum of I + K], where a [I + 1 .. I + k-1] is the overlapping part of the two, so a [I] is less than or equal to a [I + k]

D. here, K sorting is required, that is, only a [I], a [I + K], a [I + 2 K]... as long as the other group has a [J]. A [J + K], a [J + 2 K]... in this way, the array is divided into k groups, N/k elements in each group, and then each group is sorted quickly. The total cost is: theta (NLG (N/K); then read the first element of the first group, the first element of the second group .... the first element in group K; the next element in the first group, and the next element in the second group .... you can;

Here is a small detail, that is, when N/K is not an integer, take the first floor (N/K) * k elements for the above sorting, then set a [floor (N/K) * k + 1 .. elements in N] and the front [floor (N/K) * k .. [floor (N/K) * k + 1 .. n] the smallest element is inserted into the group of the retrieved element, and the rest is placed at the end;

E. sort by K-channel merging;

F. NLG (N/K) = nlgn-nlgk because K is a constant, nlgk is not considered, so the result is nlgn


8-6:







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.