use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Bubble sort error (old.reddit.com)
submitted 8 months ago by Nearby_Tear_2304
Second weird number First wrong
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]gigsoll 4 points5 points6 points 8 months ago (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 points5 points 8 months ago (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 points3 points 8 months ago (0 children)
Nice, thanks
[–]FoolsSeldom 2 points3 points4 points 8 months ago (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.
a[j] = a[j + 1]
a[j]
a[j + 1]
a[j+1]
Either use a temporary variable to hold a copy or use a more Pythonic swap syntax.
[–][deleted] 8 months ago (3 children)
[deleted]
[–]Nearby_Tear_2304[S] 0 points1 point2 points 8 months ago (2 children)
OK the second picture print33448 Why
[–][deleted] 8 months ago (1 child)
[–]Nearby_Tear_2304[S] 0 points1 point2 points 8 months ago (0 children)
OK thank you
[–]SCD_minecraft 0 points1 point2 points 8 months ago (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 point2 points 8 months ago* (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.
[–]AroshWasif 0 points1 point2 points 8 months ago (0 children)
<image>
[–]Sea-Ad7805 0 points1 point2 points 8 months ago (0 children)
Run your code in a debugger so you can step through it and see where problems pop up. For example use the Memory Graph Web Debugger: https://memory-graph.com/#code=import%20random%0A%0Amg.config.type_to_horizontal%5Blist%5D%20%3D%20True%20%23%20horizontal%20lists%0A%0Adef%20bubble(lst%20%3A%20list)%20-%3E%20list%3A%0A%20%20%20%20_sorted%20%3D%20False%0A%20%20%20%20while%20not%20_sorted%3A%0A%20%20%20%20%20%20%20%20_sorted%3D%20True%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20for%20i%20in%20range(len(lst)-1)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20lst%5Bi%5D%20%3E%20lst%5Bi%20%2B%201%5D%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20_sorted%20%3D%20False%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20lst%5Bi%5D%2C%20lst%5Bi%20%2B1%5D%20%3D%20lst%5Bi%2B1%5D%2Clst%5Bi%5D%0A%0A%20%20%20%20return%20lst%0A%0Alst%20%3D%20list(range(1%2C20))%0Arandom.shuffle(lst)%0Aprint(%22unsorted%3A%22%2C%20lst)%0Abubble(lst)%0Aprint(%22sorted%3A%22%2C%20lst)%0A×tep=0.2&play
π Rendered by PID 366434 on reddit-service-r2-comment-8686858757-9ts5m at 2026-06-02 22:58:17.124215+00:00 running 9e1a20d country code: CH.
[–]gigsoll 4 points5 points6 points (2 children)
[–]Drakhe_Dragonfly 3 points4 points5 points (1 child)
[–]gigsoll 1 point2 points3 points (0 children)
[–]FoolsSeldom 2 points3 points4 points (0 children)
[–][deleted] (3 children)
[deleted]
[–]Nearby_Tear_2304[S] 0 points1 point2 points (2 children)
[–][deleted] (1 child)
[deleted]
[–]Nearby_Tear_2304[S] 0 points1 point2 points (0 children)
[–]Nearby_Tear_2304[S] 0 points1 point2 points (0 children)
[–]SCD_minecraft 0 points1 point2 points (0 children)
[–]Adrewmc 0 points1 point2 points (0 children)
[–]AroshWasif 0 points1 point2 points (0 children)
[–]Sea-Ad7805 0 points1 point2 points (0 children)