冒泡排序 python实现

def bubble_sort(list_1):
    n = len(list_1)
    for j in range(n-1):
        for i in range(0,n-1-j):
            if list_1[i]>list_1[i+1]:
                list_1[i],list_1[i+1] = list_1[i+1],list_1[i]

if __name__ == '__main__':
    list_1 = [2,23,42,1,9]
    bubble_sort(list_1)
    print(list_1)

输出

[1, 2, 9, 23, 42]

你可能感兴趣的:(python)