[Python] python实现二分查找算法 →→→→→进入此内容的聊天室

来自 , 2020-01-07, 写在 Python, 查看 136 次.
URL http://www.code666.cn/view/64b327d4
  1. # low and high have to be integers
  2.  
  3. def binarySearch(array, key, low, high):
  4.     if low > high: # termination case
  5.         return -1
  6.  
  7.     middle = (low + high) / 2 # gets the middle of the array
  8.  
  9.     if array[middle] == key:  # if the middle is our key
  10.         return middle
  11.     elif key < array[middle]: # our key might be in the left sub-array
  12.         return binarySearch(array, key, low, middle-1)
  13.     else:                     # our key might be in the right sub-array
  14.         return binarySearch(array, key, middle+1, high)
  15.  
  16. #//python/4538

回复 "python实现二分查找算法"

这儿你可以回复上面这条便签

captcha