简体   繁体   中英

python bubble sort, what am I missing? ((

def countSwaps(a):
    count = 0
    for i in range(len(a)-1):
        for j in range(len(a)-1-i):
            count += 1
            if a[j] > a[j+1]:
                a[j],a[j+1] = a[j+1],a[j]
    return count
countSwaps()

if __name__ == '__main__':
    n = int(input())

    a = list(map(int, input().rstrip().split()))

    countSwaps(a)

Throws a runtime error

anyone can help me? what am I missing here?

thanks a lot!

def countSwaps(a):
    count = 0
    for i in range(len(a)-1):
        for j in range(len(a)-1-i):
            count += 1
            if a[j] > a[j+1]:
                a[j],a[j+1] = a[j+1],a[j]
    return count
countSwaps()

if __name__ == '__main__':
    n = int(input())

    a = list(map(int, input().rstrip().split()))

    countSwaps(a)

Returns the error

TypeError: countSwaps() missing 1 required positional argument: 'a'

So I removed the countSwaps() After which I input:

1
2 3 4 5

I first input a number in order to not fail the creation of n . Then a list of numbers divided by spaces. This works without throwing an error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM