13.1、python递归与二分查找算法

0
0
0
1. 云栖社区>
2. 博客>
3. 正文

## 13.1、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]

l.index(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]

i = 0for num in l:

if num == 66:

print(i)

i+=1

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]

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 func(l,aim):

mid = (len(l)-1)//2

if l:

if aim > l[mid]:

func(l[mid+1:],aim)

elif aim < l[mid]:

func(l[:mid],aim)

elif aim == l[mid]:

print("bingo",mid)

else:

print('找不到')

func(l,66)

func(l,6)

l1 = [1, 2, 4, 5, 7, 9]

def two_search(l,aim,start=0,end=None):

end = len(l)-1 if end is None else end

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

if end >= start:

if aim > l[mid_index]:

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

elif aim < l[mid_index]:

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

elif aim == l[mid_index]:

return mid_index

else:

return '没有此值'

else:

return '没有此值'

print(two_search(l1,9))

+ 关注