1

I am making a class for sorting algorithms . But there seems to be some error which is giving the value of my sorted linked list as None. I cannot identify the missing piece of the code. Please help.

#Bubble sort algorithm

class Sort:
        def Bubble(self,llister):
            for i in range(len(llister)-1,0,-1):
                for j in range(i):
                    if llister[j]>llister[j+1]:
                        llister[j],llister[j+1]= llister[j+1],llister[j]

if __name__=='__main__':
    obj = Sort()
    llist = [2,5,3,15,10,13,1]

    print("The list before bubble sort : " ,llist)
    ans = obj.Bubble(llist)
    print("The list after bubble sort  :" ,ans)
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
  • 1
    You don't do return? I mean Bubble does not return anything so yes ans is None. I don't know if your implementation is correct (i don't read this part). – PauZen Sep 03 '19 at 11:49
  • you need to return the sorted list at the end of the `Bubble` function. – Derlin Sep 03 '19 at 11:50
  • Or, given that your method mutates the list in-place, simply print(llist) afterwards; returning None is the convention for mutation. – jonrsharpe Sep 03 '19 at 11:53

1 Answers1

-1

Unfortunately Python doesn't do any type checking. Your bubble function doesn't contain a return statement; it therefore returns None by default. ans is therefore None and None is what is printed.

In order to avoid returning None, return a result explicitly instead e.g. return 'Some'.

user234461
  • 1,133
  • 12
  • 29