冒牌排序

# author: Jingke
def bubble_sort(nums):
    n = 1
    for i in range(len(nums) - 1):
        print('i:%s'%i)
        for j in range(len(nums) - 1 - i):  #第i次时,有i个已经排好,剩余需要对比大小第个数
            if nums[j] > nums[j + 1]:
                nums[j], nums[j + 1] = nums[j + 1], nums[j]
        print(nums)

    return nums

list = [6, 4, 8, 3, 7, 5]
bubble_sort(list)

你可能感兴趣的:(冒牌排序)