Let's start with a guess digital game: I have a one-to-one mind ~ You can guess the number between 64 (you can only ask the answer to the question "yes" or "no ). In order to ensure that you can guess with as few times as possible under whatever circumstances, what strategy should you adopt? Obviously, binary. First, guess if it is in 1 ~ Between 32, eliminate half of the possibility, and then continue to divide the range.
Let's start with a guess digital game:
In my heart, I read a 1 ~ You can guess the number between 64 (you can only ask the answer to the question "yes" or "no ). In order to ensure that you can guess with as few times as possible under whatever circumstances, what strategy should you adopt?
Obviously, binary. First, guess if it is in 1 ~ Between 32, eliminate half of the possibility, and then continue to divide the range. This policy ensures that you can guess the number within times regardless of how the number is played by you. In terms of algorithms, the lower bound is the best. (Lower boundary of an algorithm: the lower boundary of a problem is the minimum time complexity required by any algorithm used to solve the problem .?)
Let's review the essence of this game: why does this strategy have an optimal lower bound?
The answer is also simple. this strategy is balanced. If the policy is not balanced, for example, whether the policy is in the range of 1 ~ Between 10, once found not in 1 ~ In the case of 10, there will be more possibilities than N/2.
The essence of this strategy can be summarized as "making the unknown world accessible ". It has no "weakness", and any branch of the answer is equal probability. On the contrary, once a branch contains more possibilities, you will be depressed when the situation falls onto that branch. For example, the worst way to guess a digital game is to guess one by one: Is it 1? Is it 2 ?... Because in the worst case of this method, it takes 64 times to guess, and the lower bound is very bad. The reason why a binary search is good is that it removes half of the possibility every time and can eliminate half of the possibility in any case (it is the best performance in the worst case ).
Time complexity of digit prediction
The time complexity of number guessing is actually the complexity of binary search.
The basic idea of binary search is to divide n elements into roughly equal two parts, and then compare them with x using a [n/2]. if x = a [n/2], if x <a [n/2] is found, the algorithm is aborted. if x <a [n/2], search for x in the left half of array a. If x> a [n/2], search for x in the right half of array.
Therefore, the time complexity of binary search is nothing more than the number of while loops.
We can follow the loop to think about it. There are n elements in total, followed by n, n/2, n/4,..., n/2 k, where k is the number of cycles.
For each comparison, the search range is reduced to 1/2. when the range length is shortened to 1, the search is completed, and then the number of times is compared to the mathematical problem in high school.
The Other n/2 k = 1, that is, 2 k = n, so k = log2n.
K is the number of times, and the time complexity can be expressed as O () = O (logn ).
Additional reading
The topic list of this article is as follows:
- Knowledge in quick sorting: start with guesses
- Knowledge in quick sorting: Let's look at the question of ball.
- Knowledge in quick sorting: Information entropy
- Knowledge in quick sorting: the process of quick sorting
- Knowledge in quick sorting: Hall and quick sorting
- Knowledge in quick sorting: Implementation of Hall's fast sorting
- Knowledge in quick sorting: Key element selection and algorithm efficiency
- Knowledge in quick sorting: randomization fast sorting
Address: http://www.nowamagic.net/librarys/veda/detail/2387,welcome.