Skip to content

Instantly share code, notes, and snippets.

@YK-Unit
Last active September 8, 2023 23:28
Show Gist options
  • Save YK-Unit/c003d5c0c8b78c0ed5589a3d09b57aec to your computer and use it in GitHub Desktop.
Save YK-Unit/c003d5c0c8b78c0ed5589a3d09b57aec to your computer and use it in GitHub Desktop.
代码的简介
def bubble_sort(lst):
n = len(lst)
for i in range(n):
for j in range(0, n - i - 1): # 最后一个元素已经在适当位置了
if lst[j] > lst[j + 1]: # 交换如果当前元素比下一个元素大
lst[j], lst[j + 1] = lst[j + 1], lst[j] # 交换元素
return lst
# 测试代码
lst = [64, 34, 25, 12, 22, 11, 90]
print("原始列表:", lst)
bubble_sort(lst)
print("排序后的列表:", lst)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment