Python bubble sort algorithm

def bubble_sort(nums):
    for i in range(len(nums) - 1):  # 这个循环负责设置冒泡排序进行的次数
        for j in range(len(nums) - i - 1):  # j为列表下标
            if nums[j] > nums[j + 1]:
                nums[j], nums[j + 1] = nums[j + 1], nums[j]
    return nums
 
print(bubble_sort([38, 28, 9, 31, 20, 23, 17, 98]))

Guess you like

Origin blog.csdn.net/weixin_44123630/article/details/115039587