# 冒泡defmaopao(alist):for i inrange(len(alist)-1):for j inrange(len(alist)-1-i):if alist[j]>alist[j+1]:
alist[j],alist[j+1]=alist[j+1],alist[j]print(alist)
maopao([3,5,2,8,1,9,4,7])# 快排defquick(list):iflen(list)<2:returnlist
tmp =list[0]# 临时变量 可以取随机值
left =[x for x inlist[1:]if x <= tmp]# 左列表
right =[x for x inlist[1:]if x > tmp]# 右列表return quick(left)+[tmp]+ quick(right)
li =[4,3,7,5,8,2]print quick(li)# 二分 (非递归的方式实现)deffunc(list1,item):
first=0
end=len(list1)-1while first<=end:
mid=(first+end)//2if item==list1[mid]:returnTrueelif item>list1[mid]:
first=mid+1else:
end=mid-1returnFalseprint(func([i for i inrange(1,101)],1))
排序算法# 冒泡def maopao(alist): for i in range(len(alist)-1): for j in range(len(alist)-1-i): if alist[j]>alist[j+1]: alist[j],alist[j+1]=alist[j+1],alist[j] print(alist)maopao([3,5,2,8,1,9,4,7])# 快排def qu