冒泡排序,递归二分查找法,二分查找法


#冒泡排序
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 start<=end: mid=(start+end)//2 if item==list1[mid]: return mid elif item>list1[mid]: start=mid+1 else: end=mid-1 return -1 print(erfengfa(0,len(list1)-1,30,list1))

  

posted @ 2019-06-28 22:30  爱学习的小猫咪  阅读(184)  评论(0编辑  收藏  举报