Semi-query is a binary query. The query time complexity is O (logn), which is much more efficient than sequential query. The only requirement is that the tables to be queried have been sorted.
1. equi-half lookup is relatively simple, and the algorithm is as follows:
Def binarysearch (data, value): Low = 0 High = Len (data)-1 while low <= high: middle = (high-low) /2 + low # This processing can prevent integer addition overflow if data [Middle] = value: return middle # locate and return the subscript if data [Middle] <value: low = middle + 1 else: High = middle-1 return-1 # No returned-1
Pay attention to the following points for equi-fold searches:
1) The cyclic condition is low <= high, not low
The search process is as follows:
After two queries, low = high is equal to 1, so the loop jumps out and-1 is returned. In fact, 1 should be returned because data [1] = 4
2) Low = middle + 1 instead of low = middle. If it is written as low = middle, it will lead to an endless loop. As an example, we look for 8 in the above data table,
The search process is as follows:
We can see that after 4th searches, it is in an endless loop, and low is always equal to 4.
3) High = middle-1, which is similar to 2). Writing high = middle will lead to an endless loop. You can try to search for 3 in the data table.
4) Middle = (high-low)/2 + low, why not write it as middle = (high + low)/2. If both high and low are of the int type, high = 30000, low = 20000, high + low = 50000 has exceeded the int type range. divide it by 2 and the middle is no longer the expected number, while middle = (high-low)/2 + low can handle this potential overflow error.
2. Non-equivalent half-Query
Most of the time, we need to perform range search in the sequence table. For example, we need to find the number of values smaller than a certain value in data, data [Index] <value, which is different from the Equi-fold lookup, in non-equivalent search, even if no value is found in the data of the ordered table, a subscript rather than-1 may be returned, as shown in the previous data query <4.5, we should make the half lookup return 1, because all I <= 1 meets data [I] <4.5, although 4.5 Is not in data.
In fact, you only need to add several judgment conditions to the Equi-fold query. The following is a python algorithm:
1 # Half-fold range search 2 # if the search is successful, the maximum subscript is returned, meeting the requirements of data [I] <value or data [I] <= value 3 # If the specified range does not exist, -1 4 def binarysearch (data, compare, value): 5 length = Len (data) 6 low = 0 7 High = length-1 8 while low <= high: # equi-fold query value 9 Middle = (high-low)/2 + low10 if data [Middle] = value: 11 break # Find value, skip loop 12 if data [Middle] <value: 13 low = middle + 114 else: 15 high = middle-116 If low <= high: # value in data 17 if compare = '<=' or compare = '> =' or compare = ': # Compare the values that contain' =, the subscript 18 return middle19 Elif compare = '<': # '<' must return the first subscript, which may be-120 return middle-121 else: 22 # (compare = '>') '>' you need to return a subscript 23 if middle = length-1: # When middle is equal to the length of an ordered table, indicates that data [I]> value does not exist.-124 return-125 else: 26 return middle + 1 # return the following subscript 27 else: # value is not in data, at this time, high = low-1. If the search is successful, the value is 28 If compare = ': 29 return-1 # equivalent search in the open interval of (high, low, return-1, search failed 30 Elif compare = '<' or compare = '<=': # returns the left endpoint of the range, and may return-131 return high32 else: 33 # Compare = '>' or compare = '> =' 34 if low> = Length: # returns-135 return-136 else: 37 return low # Return range right endpoint
The above algorithm implements a general non-equivalent half-query. First, perform an equivalent half-query to find the position or range of the value, as shown in <4.5, the range of the Equi-fold query is (high, low) = (1, 2), and all the indexes found are 1, that is, the return value is 1.
Whether the value affects the search results of "<=", "> =" and "=" in data, all the points are processed.
Call:
1 data = [5,6,7,8,9,10,11,12,13,14,15,16]2 print binarySearch(data,‘>‘,11)3 print binarySearch(data,‘<=‘,5)
[Reprint]
Http://www.cnblogs.com/fengfenggirl/archive/2013/06/14/3136816.html