美文网首页
Day18 二分查找

Day18 二分查找

作者: 快乐的老周 | 来源:发表于2020-06-09 23:47 被阅读0次
def bin_search(mylist, target):
   if len(mylist) == 0:
       return False
   mid = len(mylist) //2
   min = 0
   max = len(mylist) -1
   while mylist[mid] != target:
       print(mylist[mid])
       if mylist[mid] >= target:
           max = mid
           mid = (min + max) //2
       else:
           min = mid
           mid = (min + max) //2
   return mid

def test_bin_search():
   mylist = [1,2,3,4,5,6,7,8,9,11,13,15,16,17,19,21,23]
   target = 1
   assert bin_search(mylist, target) == 0

if __name__ =='__main__':
   mylist = [1,2,3,4,5,6,7,8,9,11,13,15,16,17,19,21,23]
   target = 8
   print(bin_search(mylist,target))

相关文章

网友评论

      本文标题:Day18 二分查找

      本文链接:https://www.haomeiwen.com/subject/imvytktx.html