算法:冒泡排序、快速排序、堆排序、选择排序、归并排序、插入排序

冒泡排序

# 冒泡排序
list = [7,3,5,1,9]
for i in range(1,len(list)):
    for j in range(0,len(list)-1):
        if list[j] > list[j+1]:
            list[j],list[j+1]=list[j+1],list[j]
            print(list)

选择排序

# 选择排序
list = [5, 6, 3, 8, 9, 1, 7, 2, 0, 4]
a = 0
while a < len(list):
    a += 1
    b = list.index(max(list[a-1:]))
    list[b], list[a-1] = list[a-1], list[b]
    print(list)
Last modification:July 23rd, 2020 at 11:54 pm
如果觉得我的文章对你有用,请随意赞赏