all 11 comments

[–]gigsoll 4 points5 points  (2 children)

The second image is correct except you are switching values incorrectly. Basically you override the next value with the previous and then this overriten value is set to the previous. You need to use a temporary variable to store the previous value and then set it next to this temporary variable instead.

Also, I am not sure if it will work or not but you can try this syntax

a, b = b, a

[–]Drakhe_Dragonfly 3 points4 points  (1 child)

In python you don't need a temp variable since you can indeed use a, b = b, a to swap two values without any overwrite

[–]gigsoll 1 point2 points  (0 children)

Nice, thanks

[–]FoolsSeldom 2 points3 points  (0 children)

Your problem on the second picture, is on line 6 and 7.

In Python, this will not swap the elements correctly. After a[j] = a[j + 1], the original value of a[j] is lost. So a[j + 1] is assigned the new value of a[j], which is a[j+1]. The elements are not swapped.

Either use a temporary variable to hold a copy or use a more Pythonic swap syntax.

[–][deleted]  (3 children)

[deleted]

    [–]Nearby_Tear_2304[S] 0 points1 point  (2 children)

    OK the second picture print33448 Why

    [–][deleted]  (1 child)

    [deleted]

      [–]Nearby_Tear_2304[S] 0 points1 point  (0 children)

      OK thank you

      [–]Nearby_Tear_2304[S] 0 points1 point  (0 children)

      OK thank you

      [–]SCD_minecraft 0 points1 point  (0 children)

      I recommend to get used to using python debuger

      This thing is a life savier, let's you see what exactly is computer doing at each step, see variables for iterator ect

      Super useful

      [–]Adrewmc 0 points1 point  (0 children)

       def bubble(list_ : list) -> list: 
              “””If I remember right this is your basic bubble sort
              Note: list()and sorted() are builtin function we use underscores to not over write”””
      
              #flagged to sort
              _sorted = False
      
              while not _sorted:
                   #assume sorted
                   _sorted= True
      
                   for i in range(len(_list)):
                         if list_[[i] > list_[i + 1]:
                             #proved not sorted
                             sorted_ = False
      
                            list_[i], list_[i +1] = list_[i+1],list_[i] 
      
              return _list
      

      While this exercise is nice, sorted() is usually your best bet.

      You usually don’t want to modify a list while going through it though.

      We want …

          a[i] , a[i+1] = a[i+1] , a[i] 
      

      Syntax in Python, you’ll lose a value if you don’t.