冒泡排序,递归二分查找法,二分查找法
2020-12-13 04:10
标签:class 递归 top color lis tar range list star 冒泡排序,递归二分查找法,二分查找法 标签:class 递归 top color lis tar range list star 原文地址:https://www.cnblogs.com/wutanghua/p/11105090.html
#冒泡排序
list1=[2,4,5,6,7,8,9,11,30,35,38,41,42]
def bubble_sort(list):
for i in range(len(list)-1):
for j in range(len(list)-1-i):
if list[j]>list[j+1]:
list[j],list[j+1]=list[j+1],list[j]
print(list)
list1=[2,4,55,6,78,38,95,11,30,35,38,41,42]
bubble_sort(list1)#递归二分查找法
list1=[2,4,5,6,7,8,9,11,30,35,38,41,42]
def erzcf(start,stop,item,list1):
mid=(start+stop)//2
if start>stop:
return -1
if item ==list1[mid]:
return mid
elif item>list1[mid]:
return erzcf(mid+1,stop,item,list1)
else:
return erzcf(start,mid-1,item,list1)
print(erzcf(0,len(list1)-1,30,list1))
#二分法
list1=[2,4,5,6,7,8,9,11,30,35,38,41,42]
def erfengfa(start,end,item,list1):
while startlist1[mid]:
start=mid+1
else:
end=mid-1
return -1
print(erfengfa(0,len(list1)-1,30,list1))