用python实现冒泡排序和选择排序(Python经典编程案例)

1. 冒泡排序:
def bubble_sort(list):
    for i in range(0, len(list)):
        is_sorted = True
        for j in range(0, len(list) - i - 1):
            if list[j] > list[j + 1]:
                list[j], list[j + 1] = list[j + 1], list[j]
                is_sorted = False
        if is_sorted:
            return


list1 = [97, 3, 6, 1, 8, 5, -20, 100, 50, 200, -32, 123]
bubble_sort(list1)
print(list1)

执行结果如下图:
用python实现冒泡排序和选择排序(Python经典编程案例)_第1张图片

2. 选择排序:
def choose_sort(list):
    list_len = len(list)
    for i in range(0, list_len):
        for j in range(i + 1, list_len):
            if list[i] > list[j]:
                list[i], list[j] = list[j], list[i]
                

list1 = [3, 6, 1, 8, 5, -20, 100, 50, 200]
choose_sort(list1)
print(list1)

执行结果如下图:
用python实现冒泡排序和选择排序(Python经典编程案例)_第2张图片

你可能感兴趣的:(python经典编程案例)