Back to General discussions forum
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
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.