【问题标题】:Bubble sort error, whats wrong in my code冒泡排序错误,我的代码有什么问题
【发布时间】:2022-07-16 22:33:20
【问题描述】:

我正在学习python并昨天开始使用冒泡排序,但我似乎找不到错误,我一直在试图找到错误,但我没有足够的知识来找到它。如果有人可以指导我,那就太好了:

class BubbleSort1:

def __init__(self) -> None:
    pass

def read(self):
    lst = None
    lst = []
    n1 = int(input('Enter the number of values to be sorted:'))
    print('Enter the values to be sorted')
    for i in range(0,n1):
        ele = int(input())

        lst.append(ele)
    print('Unsorted list:')
    print(lst)

def sort(self,lst):
    for i in range(len(lst)-1,0,-1):
        for j in range(i):
            if lst[j] > lst[j+1]:
                temp = lst[j]
                lst[j] = lst[j+1]
                lst[j+1] = temp
def display(self,lst):
    print('sorted list')
    print(len(lst))
object1 = BubbleSort1()
object1.read()
object1.sort()
object1.display()

错误是

> Enter the number of values to be sorted:5
>     Enter the values to be sorted
>     5
>     4
>     3
>     2
>     1
>     Unsorted list:
>     [5, 4, 3, 2, 1]
>     Traceback (most recent call last):
>       File "c:\Users\User1\OneDrive\Desktop\New folder\copy", line 31, in <module>
>         object1.sort()
>     TypeError: BubbleSort1.sort() missing 1 required positional argument: 'lst'

【问题讨论】:

  • 你不会将lst传递给sort()
  • 您希望类实例将列表作为属性吗?否则,你为什么还要上课?

标签: python python-3.x sorting bubble-sort


【解决方案1】:

你有两个选择:

  1. read返回lst并将lst传递给sortdisplay
  2. lst 设置为self.lst 并在self 的所有函数上使用此lst

Option_1 您不会将lst 传递给sort()display()。您可以通过从读取返回lst 并传递给sort()display() 来解决您的问题。

class BubbleSort1:

    def __init__(self) -> None:
        self.lst = []
        pass

    def read(self):
        lst = None
        lst = []
        n1 = int(input('Enter the number of values to be sorted:'))
        print('Enter the values to be sorted')
        for i in range(0,n1):
            ele = int(input())

            lst.append(ele)
        print('Unsorted list:')
        print(lst)
        return lst

    def sort(self,lst):
        for i in range(len(lst)-1,0,-1):
            for j in range(i):
                if lst[j] > lst[j+1]:
                    temp = lst[j]
                    lst[j] = lst[j+1]
                    lst[j+1] = temp
    
    def display(self,lst):
        print('sorted list')
        print(lst)
object1 = BubbleSort1()
lst = object1.read()
object1.sort(lst)
object1.display(lst)

Option_2

class BubbleSort1:

    def __init__(self) -> None:
        self.lst = []

    def read(self):
        lst = None
        lst = []
        n1 = int(input('Enter the number of values to be sorted:'))
        print('Enter the values to be sorted')
        for i in range(0,n1):
            ele = int(input())

            lst.append(ele)
        print('Unsorted list:')
        print(lst)
        self.lst = lst

    def sort(self):
        lst = self.lst
        for i in range(len(lst)-1,0,-1):
            for j in range(i):
                if lst[j] > lst[j+1]:
                    temp = lst[j]
                    lst[j] = lst[j+1]
                    lst[j+1] = temp
    
    def display(self):
        print('sorted list')
        print(self.lst)
object1 = BubbleSort1()
lst = object1.read()
object1.sort()
object1.display()

输出:

Enter the number of values to be sorted:3
Enter the values to be sorted
1
3
2
Unsorted list:
[1, 3, 2]
sorted list
[1, 2, 3]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    • 2020-08-07
    • 2023-03-29
    • 2014-07-12
    相关资源
    最近更新 更多