-2

I have the following program in Python for bubble sort which takes as input the number of passes and a unsorted list:

def bubblesort(passnum,list=[]):
    for j in range (passnum):
        for i in range (len(list)):
            if list[i-1]<list[i]:
                continue
            elif list[i-1]>list[i]:
                temp=list[i]
                list[i]=list[i-1]
                list[i-1]=temp

list=[54,26,93,17,77,31,44,55,20]
passnum=int(input("Enter the number of passes\n"))
bubblesort(passnum,list)
print(list)

But it is sorting the list on first pass but not working on subsequent passes. Can someone tell what's wrong, since i am still a beginner in Python?

I get the output:

Enter the number of passes: 2 
[54,17,77,31,44,55,20,26,93] 

Which means the first pass ran correctly because of 93 at highest index, but then 77 should be before 93.

Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50
  • 1
    You might want to look at https://stackoverflow.com/questions/1132941/least-astonishment-and-the-mutable-default-argument You also shouldn't be using `list` as a name because that is a builtin – roganjosh Jul 21 '18 at 09:02
  • @roagnjosh, that an enhancement, since i am a beginner, i am not working on enhancements, but on functions, therefore if you can kindly suggest why this function is not sorting on higher number passes. – Muhammad Ausaf Ali Yousaf Jul 21 '18 at 09:15
  • Sorry, what do you mean by "enhancement"? The link I posted is in reference to a mutable default argument, and you're also trampling on a builtin name. Your code doesn't work; do you not think it's worth considering whether these "enhancements" might be part of the solution to your problem? – roganjosh Jul 21 '18 at 09:17
  • 1
    Or, put another way, your comment amounts to "I want to code badly and still get the correct result". Please do not approach learning programming like this. – roganjosh Jul 21 '18 at 09:22
  • tell us what results do you get and what they should be in your opinion and why or it is an off-topic question, we are not here to decrypt your code and intentions – Azat Ibrakov Jul 21 '18 at 09:47
  • @Azat Ibarkov i get the output: Enter the number of passes: 2 [54,17,77,31,44,55,20,26,93] Which means the first pass ran correctly because of 93 at highest index, but then 55 should be with 93. – Muhammad Ausaf Ali Yousaf Jul 21 '18 at 09:48

1 Answers1

1

First of all as @roganjosh said

  • we should avoid setting default argument to a mutable object (only if it was our intent to do so, e.g. for some kind of caching), furthermore this default value is never used, running sort with default empty container doesn't make sense when we can explicitly pass empty list if we need, so we can simply delete default argument here,
  • we should avoid re-using built-ins names (it ends up in unpredictable behavior).

Also swap in Python can be done without temp object using tuples.

About your problem: range built-in function with single argument generates int objects from 0 to argument - 1, so in your particular case on first iteration (i = 0) you are comparing

list[-1] < list[0]

which turns to be False on the second pass (because last element is the max element), so we are replacing first and last element.

So we can end up with something like

def bubblesort(passnum, list_):
    for _ in range(passnum):
        for i in range(1, len(list_)):  # note that we are starting from 1 instead of 0
            if list_[i - 1] < list_[i]:
                continue
            elif list_[i - 1] > list_[i]:
                list_[i], list_[i - 1] = list_[i - 1], list_[i]

Test

>>> list_ = [54, 26, 93, 17, 77, 31, 44, 55, 20]
>>> passnum = int(input("Enter the number of passes\n"))
Enter the number of passes
2
>>> bubblesort(passnum, list_)
>>> list_
[26, 17, 54, 31, 44, 55, 20, 77, 93]

Further reading

Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50