Introduction to algorithms Chapter 1 exercise after class

Source: Internet
Author: User

Transferred from http://www.cnblogs.com/bigrabbit/archive/2012/06/08/2541356.html, added in the middle

7.2-4 banks often record transactions related to an account based on the transaction time. However, many people prefer to receive their bank statements according to the bill number. Therefore, how to sort by transaction time to sort by check number becomes a problem of sorting input in almost sorted order. It is proved that the performance of insert_sort is always better than that of quiksort. Problem Analysis: For quiksort, inputting an sorted array is the worst case, and each interval Division is the maximum asymmetry. The recursion time of the algorithm is T (n) = T (n-1) + merge (n), and the algorithm time complexity is merge (N ^ 2). For insert-sort, the input of an sorted array is the best case. The algorithm time complexity is O (n ). That is to say, when you input an array in almost sorted order, the fast sorting tends to be the worst, while the insert sorting tends to be the best. To reduce the probability of this worst case, use the fast sort randomization version, and the expected running time is O (nlgn ). 7.3-2 how many times has randomized_quiksort called the random sorting generator in the worst case? How many times has it been called in the best case? Give your answer in the form of a token. Problem Analysis: in random and fast sorting, as long as the number of elements in the range is greater than 1, you need to call Randomized-partition, select the principal component for Interval division, and select the principal component to call random. Once the principal component is selected, it will not be added to the subsequent sorting. Simply put, the number of times that a random number is generated when a principal component is selected. On the other hand, from the perspective of the algorithm's recursive Binary Tree tree, the non-leaf node of the recursive binary tree can represent a principal component. The leaf node can be divided into two types, one of which contains 0 elements, the other node contains one element (which is abstract and illustrated in the following figure), and the leaf node element is not a principal element ). The sum of the number of non-leaf nodes (principal components) and the number of leaf nodes containing one element is the size N of the input sequence. That is, the number of non-leaf nodes (principal components) in the recursive tree is the number of calls to the random. Number of calls to random t (random) ≤ n, that is, T (random) = O (n ). Based on the nature of the binary tree, for any binary tree, if the number of leaf nodes is A, and the number of nodes with a degree of 2 (not leaf nodes) is B, then a = B + 1; for Recursive Binary Trees in fast sorting, the number of non-leaf nodes is 2. Assume that the number of leaf nodes containing 0 elements A0 (≥0), the number of leaf nodes containing 1 element is A1, so B = A-1 ≥a1-1; and B + A1 = N, B ≥ n/2-1, that is, T (random) = Ω (n ).
In general, T (random) = random (n) My understanding: the figure below shows how to sort the numbers of 4, 3, 4, 1, 6, 5, 7, and 8 quickly and use the first element as the principal element, select 4 for the first time, and divide the series into segments 13, 4, 6, 5, 7, and 8. Select the principal component for the second time. The principal component of 1, 3, and 4 is 1. 6, 5, 7, and 8 have 6 principal components, as shown in. It can be seen that the leaf node is not the principal component. For example, 4 or 8 is not the main component, because after the array has only one element, partition does not take the principal component. Now you can take the O (N) and lead (n) exams ). Assume that the number of elements in an array is N, then n = non-leaf nodes (principal component) (,) + the number of leaf nodes containing one element, T (random) ≤ n, that is, T (random) = O (n ). Based on the nature of the binary tree, for any binary tree, if the number of leaf nodes is A, and the number of nodes with a degree of 2 (not leaf nodes) is B, then a = B + 1; then all nodes are a + B = B + 1 + B = 2B + 1> = n. therefore, B ≥
N/2-1.
In general, T (random) = random (N)

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.