[英]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:所以我删除了countSwaps()
之后我输入:
1
2 3 4 5
I first input a number in order to not fail the creation of n
.我首先输入一个数字,以免创建n
失败。 Then a list of numbers divided by spaces.然后是一个数字列表除以空格。 This works without throwing an error.这可以正常工作而不会引发错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.