Home > OS >  My Bubble sort class give error, what's wrong in the Class?
My Bubble sort class give error, what's wrong in the Class?

Time:07-16

I am learning python and started bubble sort yesterday, but I can't seem to find the error,I have been trying to find the error, but I don't have enough knowledge to find it. It would be great if someone could guide me:

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()

The error is

> 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'

CodePudding user response:

You have two options:

  1. return lst from read and pass lst to sort, display
  2. set lst to self.lst and use this lst on all function from self.

Option_1 You don't pass lst to sort() and display(). You can solve your problem with return lst from read and pass to sort() and display().

class BubbleSort1:

    def __init__(self) -> None:
        pass

    def read(self):
        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 = []
        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()
object1.read()
object1.sort()
object1.display()

Output:

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]
  • Related