冒泡算法
#列表元素两辆比较,大的值逐渐向后移动 # def sort(alist): # for i in range(len(alist)-1): # if alist[i]>alist[i+1]: # alist[i],alist[i+1]=alist[i+1],alist[i] # return alist # # alist=[1,3,5,2,6,9,8,7] # print(sort(alist)) # [1, 3, 2, 5, 6, 8, 7, 9] #逐渐将乱序序列的最大值找出放置在乱序序列的尾部 # def sort(alist): # for j in range(len(alist)-1): # for i in range(len(alist)-1-j): # if alist[i]>alist[i+1]: # alist[i],alist[i+1]=alist[i+1],alist[i] # return alist # # alist=[1,3,5,2,6,9,8,7] # print(sort(alist)) # [1, 2, 3, 5, 6, 7, 8, 9]
选择排序
#建乱序中的最大值找出,跟最后一个元素交换位置 # def sort(alist): # max_index=0 #最大值下标 # for i in range(1,len(alist)): # if alist[max_index]<alist[i]: # max_index=i # alist[len(alist)-1],alist[max_index]=alist[max_index],alist[len(alist)-1] # return alist # alist=[3,8,5,7,6] # print(sort(alist))
标签:sort,alist,range,len,算法,冒泡,排序,乱序 From: https://www.cnblogs.com/mengdie1978/p/17135026.html