【问题标题】:python bubble sort, what am I missing? ((python冒泡排序,我错过了什么? ((
【发布时间】:2020-11-11 06:35:36
【问题描述】:
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)

引发运行时错误

有人可以帮助我吗?我在这里错过了什么?

非常感谢!

【问题讨论】:

标签: python bubble-sort


【解决方案1】:
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)

返回错误

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

所以我删除了countSwaps(),然后我输入:

1
2 3 4 5

我首先输入一个数字,以免创建n 失败。然后是数字列表除以空格。这可以正常工作而不会引发错误。

【讨论】:

    猜你喜欢
    • 2017-03-02
    • 2013-03-22
    • 2018-12-22
    • 1970-01-01
    • 1970-01-01
    • 2015-09-12
    • 1970-01-01
    • 2020-08-07
    • 1970-01-01
    相关资源
    最近更新 更多