---- binary search algorithm to find the hash lookup

Binary search

  For us to achieve an ordered list of search is very useful. In order to find, when we compare with the first element, if the first element is not what we're looking for, there is a maximum of  n-1 elements need to be compared. Binary search is to start from the middle element, rather than find a list in order. If the element is the element we're looking for, we completed the look. If it is not, we can eliminate half the remaining elements using an ordered list of properties. If the element we're looking for is greater than the middle element, you can eliminate the intermediate element and the middle element is smaller than half elements. If the element in the list, certainly a large part of that half. Then we can repeat with half of the big process, continue to start from the middle element, comparing it with what we are looking for content.  

1  "" " 
2  binary search:
 3      for the ordered list or collection;
 4      found Return index;
 5      not found returns -1.
 . 6  " "" 
. 7  
. 8  
. 9  DEF binary_searc (sorted_list, Item):
 10      Low = 0
 . 11      High = len (sorted_list)
 12 is      Middle = (High + Low) // 2
 13 is      the while Low <= High:
 14          IF sorted_list [Middle] < Item:
 15              Low Middle +. 1 =
 16          elif sorted_list [Middle]> Item:
 . 17             = Middle High -. 1
 18 is          the else :
 . 19              return Middle
 20 is          Middle = (High + Low) 2 //
 21 is      return -1   # not find -1

hash lookup

  hash

    

 

Guess you like

Origin www.cnblogs.com/open-yang/p/11367068.html