Introduction to algorithms Chapter 1 linear time sorting

Source: Internet
Author: User
1. Concept 1. Comparative sorting refers to the sorting algorithm that determines the order of each element by comparing input elements.

In the worst case, any comparative sorting should be sorted by an O (nlgn) comparison.

Merge Sorting and heap sorting are incrementally optimized.

2. Non-Comparative sorting

Non-Comparative sorting refers to the sorting algorithm that uses non-Comparative operations to determine the sorting order.

For non-Comparative sorting, the lower bound O (nlgn) is not applicable.

Count sorting is a stable sorting. If the value range of n data items is [0 .. k], the running time is O (n + k), and the running space is O (n + k)

The base sorting is also a stable sorting, and another stable sorting is required as the basis. If n d-digits exist, each digit may have k values, the running time of the used stable sorting is O (n + k), then the time of the base sorting is O (D (n + k ))

The bucket sorting is also stable. When the input data is evenly distributed, the bucket sorting can be run in linear time. All elements are evenly distributed in the interval [0, 1). The interval [0, 1) is divided into N subintervals (buckets) of the same size to sort the numbers in each bucket, columns the elements in each bucket in sequence.

 

Ii. Code
# Include <iostream> # include <cmath> using namespace STD; int length_a, digit; void print (int * a, int start, int end) {int I; for (I = start; I <= end; I ++) {if (I = Start) cout <'{'; else cout <''; cout <A [I] ;}cout <'}' <Endl ;}// count sorting void counting_sort (int * a, int * B, int K) {int I, j; // initializes the C array to 0 to count int * c = new int [k + 1]; for (I = 0; I <= K; I ++) C [I] = 0; // C [J] indicates the number of times the number J appears in array a for (j = 1; j <= length_a; j ++) C [A [J] ++; // C [I] indicates the number of times that the <= I number has appeared for (I = 1; I <= K; I ++) C [I] = C [I] + C [I-1]; // initialize B to 0, B is used to output the sorting result for (I = 1; I <= length_a; I ++) B [I] = 0; For (j = length_a; j> = 1; J --) {// if the number of numbers <= A [J] is X, after sorting, a [J] should appear at position X, that is, B [x] = A [J] B [C [A [J] = A [J]; C [A [J] --;} Delete C ;} // stable_sort (int * a, int * B, int K, int d) {int I, j; // initialize the C array to 0, used to count int * c = new int [k + 1]; for (I = 0; I <= K; I ++) C [I] = 0; int * D = new int [length_a + 1]; for (j = 1; j <= length_a; j ++) {// d [J] indicates the I-digit number of the element [J] d [J] = A [J] % (INT) Pow (10.0, d) /(INT) Pow (10.0, D-1 ); // C [J] indicates the number of times the number d [J] appears in array a C [d [J] ++ ;} // C [I] indicates the number of times that the <= I number has appeared for (I = 1; I <= K; I ++) c [I] = C [I] + C [I-1]; // initialize B to 0, B is used to output the sorting result for (I = 1; I <= length_a; I ++) B [I] = 0; For (j = length_a; j> = 1; j --) {// if the number of numbers <= d [J] is X, after sorting, a [J] should appear at position X, that is, B [x] = A [J] B [C [d [J] = A [J]; C [d [J] --;} delete [] C; Delete [] D ;}// base sort void radix_sort (int * a, int * B) {int I, J; // sort each bit in sequence, from low to high for (I = 1; I <= digit; I ++) {stable_sort (a, B, 9, i); // The input is A, the output is B, and the output data should be placed in the output data for (j = 1; j <= length_a; j ++) A [J] = B [J] ;}} int main () {CIN >>> length_a >> digit; int * A = new int [length_a + 1]; int * B = new int [length_a + 1]; int I; // randomly generate length_a digit BIT data for (I = 1; I <= length_a; I ++) {A [I] = 0; while (A [I] <(INT) Pow (10.0, digit-1) A [I] = rand () % (INT) Pow (10.0, digit );} print (A, 1, length_a); // counting_sort (a, B, 9); radix_sort (a, B); print (A, 1, length_a ); delete [] A; Delete [] B; return 0 ;}

 

 

Iii. Exercise 8.1 lower time limit of Sorting Algorithm
The 8.1-1 decision tree is a binary tree. Each node indicates a possible group of elements in the order. It is consistent with the existing comparison. The comparison result is the edge of the tree. If T is a binary tree with a depth of D, T has a maximum of 2 ^ t leaves. The depth of a binary tree with L leaves is at least lgl. There will be n in the order of n elements! Different Size relationships, the decision tree must have n! Leaves, so the decision tree depth is at least lg (N !) 8.1-2 do not look at my questions about mathematics. 8.1-3 fortunately, I have answers. Otherwise, I cannot understand the meaning of the questions. First, explain the meaning of the question. For data in a group containing n elements, there can be n! Different arrangement, that is, there are n! Input. A ranking algorithm can be represented by a decision tree. For any input arrangement, start from the top of the tree and compare and adjust the corresponding content based on the prompts of the inner node, and decide whether to enter the left or right subtree. When this arrangement becomes an ordered sequence, it reaches the leaf node. This leaf node is the leaf node corresponding to the input arrangement. The length from the root node to the leaf node is the number of comparisons in this arrangement. Input with linear time refers to n! In the input arrangement, the running time of the arrangement is smaller than or equal to O (N). Assume that for a group of data containing n elements, there are n! Input arrangement, where input arrangement with linear time is m, K = m/(n !) If K <= 1/2, then the proof is true. The comparison between K, 1/N, and 1/(2 ^ N) proves that these M input arrays correspond to M leaf nodes in the decision tree respectively. A tree with H height can have a maximum of 2 ^ h leaf nodes. 2 ^ h> = m ====> h> = lg (m) if you want a decision tree to contain M leaf nodes, the height of this decision tree is at least lg (m) according to the formula of Theorem 8.1 In p98 in introduction to algorithms, h> = O (nlgn) only needs to prove lg (m) <= O (nlgn ), then K is the value that can be obtained. Make K equal to 1/2, 1/N, 1/(2 ^ N), respectively, and substitute M = K * (N !) The calculation result is omitted. These values do not meet the requirements of 8.1-4. We cannot simply combine the lower bound of the subsequence. This is not accurate. Because there may be an algorithm that is faster than "independently sorting sub-sequences. The general idea for calculating the number of comparisons is: (1) how many different types of input are counted (2) A leaf node of the decision tree corresponding to each input arrangement. (3) Calculate the height of a decision tree based on the relationship between the number of leaf nodes and the height of the tree (4) the length from the root to the leaf node is the number of input comparisons corresponding to the leaf node. For this question, we can calculate as follows: (1) Each subsequence Has k elements, so there are K! Input sorting. A total of N/K subsequences, so a total of (K !) ^ (N/K) Input arrangement (2) decision tree at least (K !) ^ (N/K) leaves (3) A decision tree with H height. A maximum of 2 ^ h leaf nodes 2 ^ h >=( K !) ^ (N/K) ====> H >=( n/2) lg (K/2) (4) for any decision tree, at least one leaf path exceeds (n/2) lg (K/2), so the lower limit of the number of comparisons is O (nlgk)

 

8.2 count sorting
8.2-1 Assume that the range of the data to be sorted is [0, K]. therefore, B is initialized to-1 A = {6 0 2 0 1 3 4 6 1 3 2 }=> c = {2 2 2 2 1 0 2 }=> C = {2 4 6 8 9 9 11 }=> B = {-1-1-1-1-1-1 2-1-1-1-1} c = {2 4 5 8 9 9 11 }=> B = {-1-1-1-1-1 2-1 3-1 0-1-1} c = {2 4 5 7 9 9 11 }=> B = {-1-1-1 1-1 2-1 3-1-1-1} c = {2 3 5 7 9 9 11 }=> B = {-1-1 1 1-1 2-1 3-1-1 6} c = {2 3 5 7 9 9 10 }=> B = {-1-1-1 1-1 2-1 3 4-1 6} c = {2 3 5 7 8 9 10 }=> B = {-1-1 1 1-1 2 3 3 4-1 6} c = {2 3 5 6 8 9 10} = => B = {-1-1 1 1-1 2 3 3 3 4-1 6} c = {2 2 5 6 8 9 10 }=> B = {-1 0 1 1-1 2 3 3 4-1 6} c = {1 2 5 6 8 9 10 }=> B = {-1 0 1 1 1 2 3 3 4 -1 6} c = {1 2 4 6 8 9 10 }=> B = {0 0 1 1 2 2 3 3 4-1 6} c = {0 2 4 4 6 8 9 10 }=> B = {0 0 1 1 2 2 3 3 3 4 6 6} c = {0 2 4 6 8 9 9} 8.2-2 auxiliary array C [J] records the number of elements smaller than or equal to I. If the sizes of several elements are equal, these elements are sequentially filled in the SORT array from the back to the front. Because the order of elements to be processed is from the back to the front ('9'), the later elements will be filled in. So it is stable sorting 8.2-3 unstable 8.2-4counting-sort (a, B, k) in step L1-L4 to find C, ANS (A, B) = C [B]-C [A-1], C [-1] = 0

 

8.3 base sorting
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
Stable sorting: insert sorting and Merge Sorting method: adds an attribute of the initial position to each element, but when the values of the two elements are the same, the position is compared, so there will be no additional space for the same two elements: O (4n) 8.3-3
(1) When d = 1, there is only one element. Sorting this element is equivalent to sorting the entire array.
(2) it is proved that when the order of 1 to D-1 is correct, the order of D is also correct.
(3) For any two numbers A and B in the array, assume that their D-bits are ad and BD respectively.
If ad <BD, A is placed before B.
If ad> BD, A will be placed behind B
If ad = BD, the relative positions of A and B remain unchanged, because they are sorted stably, which can be ensured. 8.3-4 converts Integers to n-base and sorts them again. See introduction to algorithms 8.3-48.3-5. d times is required in the worst case.

8.4 bucket sorting

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.13, 0.16, 0.20, 0.39, 0.42, 0.53, 0.64, 0.79, 0.71, 0.89 }=> A = {0.13, 0.16, 0.20, 0.39, 0.42, 0.53, 0.64, 0.71, 0.79, 0.89} 8.4-2 The worst case is O (n ^ 2) modification: Use the worst case time to O (nlgn) algorithm to process bucket insert operations 8.4-3e (x ^ 2) = 1/4*0 + 1/2*1 + 1/4*4 = 3/2e ^ 2 (x) = [1/4*0 + 1/2*1 + 1/4*2] ^ 2 = 1 ^ 2 = 18.4-4 assume that there are n buckets-sort () 1 n <-length [a] 2 for I <-1 To N3 do insert a [I] to list B [N * (a [I]. x ^ 2 + A [I]. y ^ 2)] 4 for I <-0 to n-15 do sort list B [I] with insertion sort6 concatenate the Lists B [0], B [1],..., B [n-1] together in order8.4-5

No. I found an answer online.
X is suitable for the distribution of P, which is not necessarily a uniform distribution and is not limited. However, the p (x) value belongs to [0, 1], and X is strictly monotonically incrementing. The P (x) value is sorted by X. P (x) is evenly divided into N project groups, because X is randomly selected, so the probability of falling into each limitation is the same.
If (I-1)/n <= P (xi) <I/N, place it in the I bucket.
Http://www.mysjtu.com/page/m0/s696/696126.html4: 8-1 comparison of the average condition lower limit of sorting

A) n different elements correspond to n! Different input, so only n! The leaf nodes corresponding to the input values are probabilistic, and the rest are all 0. Because n! The occurrence of input is equal probability, and each type is 1/N1. Therefore, the probability of the corresponding leaf node is also 1/n! B) Let T (n) be the depth of a leaf node n on decision tree T, RT (N), LT (N) the depth of N on the right and left subtree of T. Then T (n) = RT (n) + 1 or T (n) = LT (n) + 1 so d (t) = D (RT) + d (LT) + Kc) the following questions cannot be understood with my limited IQ and mathematical abilities, nor can I understand the online answers.

 

8-2 sorted by linear time in-situ Conversion
A) Count sort B) Fast sort c) stable sort D) the sorting method used by the base sort requirement is satisfied (condition 2), if you want to in O (BN) the time used for completing the tasks within the specified time must be O (N) (condition 1), so only a Can e) unstable counting-sort (A, K) 1 For I <-0 to K 2 do C [I] <-0 3 for j <-1 to length [a] 4 C [A [J] <-C [A [J] + 1 5 CNT <-1 6 for I <-1 to K 7 while C [I]> 0 8 A [CNT] <-I 9 C [I] <-C [J]-110 CNT <-CNT + 1
8-3 sort data items of different lengths. See introduction to algorithms-8-3-Sort data items of different lengths.
A) Sort O (n) by the number of digits by the counting sorting algorithm, and then sort O (n) B by the base sorting method to recursively sort the elements in each bucket by count, sort by the first letter, put the same first word in the same group, and then use the Count sorting method for each group to compare the second letter. It is also possible to see someone using a dictionary tree.
8-4 kettle
A) the most primitive method of comparison: Compare all the red kettles with all the blue kettles in turn C) the algorithm is similar to the fast sorting, because the comparison is not the same color between the kettles, so use cross comparison Step 1: select a random Step 2 from the red bottle: divide the blue bottle with the red bottle as the main element Step 3: divide the result is two arrays, and get the same size as the red bottle of the blue bottle step4: Take this blue bottle as the main yuan, the red bottle is divided into Step 5: Use Step 1 to step 4 Process continuous Iteration
8-5 for average sorting, see Introduction to algorithms 6.5-8 heap sorting-K-path merge.
A) 1 sorting is full sorting B) 1, 2, 4, 5, 7, 6, 8, 9, 10 C) simply expand the division of labor to d) Step 1: Put 1, 1 + K, 1 + 2 k, 1 + 3 K ,......; 2, 2 + k, 2 + 2 K, 2 + 3 K ,......;...... As a separate set Step 2: Sort each set at O (NLG (N/K) e) Step 1: Same d) Step 1 Step 2: Merge K paths using heap sorting
8-6 merge lower bounds of sorted lists
A) 2N number. N numbers are randomly selected. The optional methods include C (2n, n) B) 2 ^ h> = C (2n, n) ====> h> = lg (2n )!) -2lg (N !) ====> H >=2nlg2n-2 nlgn ====> H >=2nlg2 can only be pushed to this, and finally how to calculate it, I also hope that experts can give advice c) d) apparently, I don't know how to prove it.

 

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.