I understand binary search

Today brush leetcode time to do several channels and binary search (BS) related to the topic and found that there are two main problems: Some questions are not very intuitive to know with BS; BS with up bad corner case processing. Here I will summarize the usage and code templates BS.

1. When can algorithm BS?

Here at Daniel Knuth quote from the words - "Although the basic idea of ​​binary search is comparatively straightforward, the details can be surprisingly tricky ...". This means that BS algorithm sounds intuitive, but the details can be very tricky. He, in his book "The Art of Computer Programming, Volume 3: Sorting and Searching" wrote "thought binary search method in 1946, it was brought up but the first one did not Bug the binary search method only in 1962. appears. "BS algorithm can also be seen is not what we thought it would be easy. What algorithm can be used when half of it? Usually occurs when an ordered array of problems we usually think can not be solved by BS, this inertia of thinking leads us to mistakenly think that "order" is a necessary condition for the application of BS, it is not true, for example, we can use the same algorithm in an array BS [4, 3, 2, 1, 5, 6, 7, 8, 9] 5 accurately locate the element. 5 is a Partition, that the array is divided into two portions of less than 5 and greater than 5, if an element is greater than 5, there must be left at the element 5, is less than 5, 5 must have the right of the element. So it seems BS is a special exclusion, specifically used to find this partition is! ! ! We use formal language simple definition of what (probably not very strict).

There interval [start, end), and is defined in this section the mapping f and a condition A, assume that a part of [start, end) at the point x, such that the point x on the left for the map f satisfies the condition A, x point to the right of the map does not satisfy the condition a (here, in turn, have no problem, as the conditions are not satisfied a full x itself can be), if you know the conditions a and f, you can find a point x by half come.

 Half is used to find the original partition in an ordered array, each point is a partition, it can be used to find. In the example [4, 3, 2, 1, 5, 6, 7, 8, 9], partition 5 is only one element.

 

2. BS algorithm templates

BS loop judgment condition Some people like to write left <= right, when we must consider is the final return of the return left or right, so I'm not used to write, think or use the left <right computer more in line with common sense, here left and right initial It refers to the corresponding [start, end) two endpoints.

 1 def BS_template():
 2     left, right = start, end
 3     while left < right:
 4         # C++ 用 mid = left + ((right - left) >> 1), 否则可能加法溢出
 5         mid = (left + right) >> 1
 6         if f(mid) match A:
 7             right = mid
 8         else:
 9             left = mid + 1
10     return left

 

Guess you like

Origin www.cnblogs.com/wory/p/12424169.html