二分法python算法_python--二分法算法

#二分法查找算法,必须处理有序的算法列表

"""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("discover!!",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

mid_index = (end - start) // 2 + start

if start <= end:

if l[mid_index] < aim:

return find(l,aim,start = mid_index + 1,end = end)

elif l[mid_index] > aim:

return find(l,aim,start=start,end=mid_index-1)

else:

return mid_index

else:

return '找不到'

ret = find(l,66)

print(ret)"""

#斐波那契 # 问第n个斐波那契数是多少#1, 1, 2, 3, 5, 8 # fib(6) = fib(5) + fib(4)

"""def fib(n,a=1,b=1):

if n==1 : return a

return fib(n-1,b,a+b)

print(fib(5))"""

#阶乘

"""def fac(n):

if n == 1 :

return 1

return n * fac(n-1)

print(fac(100))"""

你可能感兴趣的:(二分法python算法)