算法学习
作者:互联网
#二分法
def erfenfa(lis,target):
start,end=0,len(lis)-1
while True:
if end-start<=1:
if target==lis[start]:
return start
elif target==lis[end]:
return end
else:
return -1
mid = (start+end)//2
if lis[mid] <=target:
start = mid
else:
end = mid
lis=[1,2,3,5,7,8]
print(erfenfa(lis,9))
# 快速排序法
def quick(arr):
if not arr:
return []
mid = arr[0]
left = quick([i for i in arr[1:] if i<mid])
right = quick([i for i in arr[1:] if i>=mid])
return left + [mid] + right
lis = [1,3,5,7,2,43,4,8,5,3,1]
print(quick(lis))
标签:erfenfa,二分法,end,mid,学习,start,算法,lis 来源: https://www.cnblogs.com/johnsonbug/p/15978840.html