python: 冒泡排序

lst1 = [1, 5, 6, 78, 95, 34, 56, 51, 5, 25, 55, 4, 5, 45, 25, 44, 54, 4]


def bubble_sort(lst):
    """
    冒泡排序
    :param lst: input list;
    :return: sorted list;
    """
    for i in range(len(lst) - 1):  # [len(lst) - 1]times loop;
        for j in range(len(lst) - 1 - i):  # Gets two elements from the list;
            if lst[j] > lst[j + 1]:  # Judge and take big numbers;
                lst[j], lst[j + 1] = lst[j + 1], lst[j]  # Big numbers sort to last;
    return lst

if __name__ == '__main__':
    print(bubble_sort(lst1))

posted @ 2021-10-20 10:09  Annzi-Py  阅读(35)  评论(0)    收藏  举报