def buble(nums):
lenth = len(nums)
for i in range(lenth):
is_sorted = True
for j in range(1, lenth - i):
if (nums[j] < nums[j - 1]):
nums[j], nums[j - 1] = nums[j - 1], nums[j]
is_sorted = False
if is_sorted:
break
return nums
if __name__ == '__main__':
nums = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
print(buble(nums))
运行结果
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]