python 排序算法之 二分查找

def binary_search(find, L) :
  low = 0
  high = len(L)
  while low < high :
    mid = int((low + high) / 2)

    if L[mid] == find :
      return mid
    #左半边
    elif L[mid] > find :
      high = mid - 1
    #右半边
    else :
      low = mid + 1
  #未找到返回-1
  return -1

L = [1,2,3,4,5,6,7,8,9,10,11]
result = binary_search(12, L)
print(result)

    原文作者:查找算法
    原文地址: https://blog.csdn.net/qugemingzhennimanan/article/details/78081128
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞