# 冒泡排序
li=[54,26,93,17,77,31,44,52,20]
def bubble_sort(alist):
for j in range(len(alist)-1,0,-1):
for i in range(j):
if alist[i]>alist[i+1]:
alist[i+1],alist[i] = alist[i],alist[i+1]
bubble_sort(li)
print(li)
选择排序
def selection_sort(alist):
n = len(alist)
# 需要进行n-1次选择操作
for i in range(n-1):
# 记录最小位置
min_index = i
# 从i+1位置到末尾选择出最小数据
for j in range(i+1, n):
if alist[j] < alist[min_index]:
min_index = j
# 如果选择出的数据不在正确位置,进行交换
if min_index != i:
alist[i], alist[min_index] = alist[min_index], alist[i]
alist = [54,26,93,17,77,31,44,52,20]
selection_sort(alist)
print(alist)