Why does my code work? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

Why does my code work?

I tried to implement a bubble sort, but I do not understand how the code works without doing additional 'sweeps' (in the event the numbers are not sorted within a single pass.) https://code.sololearn.com/cBAb6vxOo3ES/?ref=app

17th Feb 2020, 7:09 PM
ren[paused]
ren[paused] - avatar
5 ответов
+ 1
a.sort() actually sorts your list a inplace. That means it is automatically sorting it - not your algorithm. Change to if sorted(a) == a: ...
17th Feb 2020, 7:19 PM
Russ
Russ - avatar
+ 1
Yeah, as ~ swim ~ said, something like this: while sorted(a) != a: for ...
17th Feb 2020, 8:36 PM
Russ
Russ - avatar
+ 1
Ok cool I was worried about it terminating too soon so I should use a while loop then
17th Feb 2020, 8:50 PM
ren[paused]
ren[paused] - avatar
+ 1
Thanks for the assist- I updated my code to reflect how many sweeps it took to complete also updated code as it was counting each swap and not an entire for loop as a round
18th Feb 2020, 4:51 PM
ren[paused]
ren[paused] - avatar
0
Noted and updated- now my code is back to not working like it should be :)
17th Feb 2020, 8:06 PM
ren[paused]
ren[paused] - avatar