python 实现冒泡排序

python实现排序算法:

list1 = [13, 654, 965, 784, 2326, 945, 3231]
list_length = len(list1)
for i in range(list_length):
    for j in range(i + 1, list_length):
        if list1[i] > list1[j]:
            list1[i], list1[j] = list1[j], list1[i]
print(list1)
# 输出
[13, 654, 784, 945, 965, 2326, 3231]

Process finished with exit code 0

当然直接调用python的sort()、sorted()会更加简单

猜你喜欢

转载自blog.csdn.net/darkman_ex/article/details/80740574