Promblem 027 - Bubble Sort - Check the amount of passes

Back to General discussions forum

Curio_Amigo     2020-08-02 08:32:17

I am encountering problems with the amount of passses required to sort an array. I've already reviewed my code and I can't find where the error is. If anyone can help I am very grateful. By the way, in my code the amount of swaps is conditioned by the amount of passses, and this number always matches the answer, which is not the case with the amount of passses.

Thanks a lot

Rodion (admin)     2020-08-02 09:20:45
User avatar

Hi Amigo! It looks like you already solved the task? Or you may mean that it gives incorrect solution sometimes?

I checked your code, it's almost ok except one interesting thing:

for i in range(0, len(v1)):
    # ...
        i -= 1

You have i as index of a for loop, but you are trying to modify it inside the loop yourself.

I'm almost sure this doesn't work in Python :)

Variable i is assigned anew on every iteration from iterator created by range and so your changed value is overwritten

You may try running simle example:

for i in range(10):
    print(i)
    i += 1

Thus in such cases you should prefer while loop, I think.

Please login and solve 5 problems to be able to post at forum