实施掉期数量和比较

时间:2019-04-04 02:45:11

标签: python bubble-sort

def bubbleSort(randnums):
    swpcount, compcount = 0, 0
    for numbers in range(len(randnums)-1,0,-1):
        for i in range(numbers):
            compcount +=1
            if randnums[i]>randnums[i+1]:
                swpcount +=1
                mash = randnums[i]
                randnums[i] = randnums[i+1]
                randnums[i+1] = mash
    return swpcount, compcount
swpcount, compcount = bubbleSort(randnums)

当我运行该函数时,交换次数返回0。比较次数是正确的。如何返回正确的掉期数量?

0 个答案:

没有答案