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
5 Respuestas
+ 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:
...
+ 1
Yeah, as ~ swim ~ said, something like this:
while sorted(a) != a:
for ...
+ 1
Ok cool I was worried about it terminating too soon so I should use a while loop then
+ 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
0
Noted and updated- now my code is back to not working like it should be :)