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))
本文来自博客园,作者:Annzi-Py,转载请注明原文链接:https://www.cnblogs.com/annzi/p/15427270.html

浙公网安备 33010602011771号