# 计算方法:人脑复杂,计算机简单
#查找:找数据
#排序
#最短路径
#我们学习的算法都是过去时
#要了解基础的算法,才能创造出更好的算法
#不是所有的事情都能套用现成的方法解决的
# 有些时候会用到学过的算法只是来解决问题
#二分查找算法 必须处理有序的列表

l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]
# def find(l,aim):
#     mid_index = len(l) // 2
#     if l[mid_index] < aim:
#         new_l = l[mid_index + 1:]
#         find(new_l,aim)
#
#     elif l[mid_index] > aim:
#         new_l = l[:mid_index]
#         find(new_l,aim)
#     else:
#         print('get it',mid_index,l[mid_index])
#
# find(l,66)

l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]
# def find(l,aim,start=0,end=None):
#     end = len(l) if end is None else end  #end len(1)  24
#     mid_index = (end - start) // 2 + start  #计算中间值  12 +0 =12
#     if l[mid_index] < aim:     #l[12] < 44    41 <44
#          find(l,aim,start =mid_index + 1,end=end)  #find(1,44,start=13,end=24)
#     elif l[mid_index] > aim:
#         find(l,aim,start =start + 1,end=mid_index-1)
#     else:
#         print('got it',mid_index,aim)
# #第二次
# def find(l,aim,start=0,end=None):   #1,44,start=13,end=24
#     end = len(l) if end is None else end  #end=24
#     mid_index = (end - start) // 2 + start  #计算中间值  24-13 =11 /2 =5 +13=18
#     if l[mid_index] < aim:     #l[18] < 44   67 <44
#          find(l,aim,start =mid_index + 1,end=end)
#     elif l[mid_index] > aim:   67 >44
#         find(l,aim,start =start + 1,end=mid_index-1)#find(1,44,start=13,end=17)
#     else:
#         print('got it',mid_index,aim)
# #第3次
# def find(l,aim,start=0,end=None):   #1,44,start=13,end=17
#     end = len(l) if end is None else end  #end=17
#     mid_index = (end - start) // 2 + start  #计算中间值  17-13 =4 /2 =2 +13=15
#     if l[mid_index] < aim:     #l[15] < 44   55 <44
#          find(l,aim,start =mid_index + 1,end=end)
#     elif l[mid_index] > aim:   55 >44
#         find(l,aim,start =start + 1,end=mid_index-1)#find(1,44,start=13,end=14)
#     else:
#         print('got it',mid_index,aim)
# #第4次
# def find(l,aim,start=0,end=None):   #1,44,start=13,end=14
#     end = len(l) if end is None else end  #end=14
#     mid_index = (end - start) // 2 + start  #计算中间值  14-13 =1/2 =0 +13=13
#     if l[mid_index] < aim:     #l[13] < 44   42 <44
#          find(l,aim,start =mid_index + 1,end=end)#find(1,44,start=14,end=14)
#     elif l[mid_index] > aim:   55 >44
#         find(l,aim,start =start + 1,end=mid_index-1)
#     else:
#         print('got it',mid_index,aim)
#第5次
def find(l,aim,start=0,end=None):   #1,44,start=14,end=14
    end = len(l) if end is None else end  #end=14
    if start <= end:
        mid_index = (end - start) // 2 + start  #计算中间值  14-14 =0/2 =0 +14=14
        if l[mid_index] < aim:     #l[14] < 44   43<44
             return find(l,aim,start =mid_index + 1,end=end)#find(1,44,start=15,end=14)
        elif l[mid_index] > aim:
            return find(l,aim,start =start + 1,end=mid_index-1)
        else:
            return mid_index,aim
    else:
            return 'not get it'
l = [2,3,5,10,15,16,18,22,26,30,32,35,41,42,43,55,56,66,67,69,72,76,82,83,88]

ret = find(l,66)
print(ret)

 

posted on 2019-01-31 16:06  Tim-code  阅读(181)  评论(0编辑  收藏  举报