I have been viewing the article Cycle sort on Wikipedia which has an implementation in python which I don't understand.
https://en.wikipedia.org/wiki/Cycle_sort
What do the lines like this do?
array[pos], item = item, array[pos]
I have been viewing the article Cycle sort on Wikipedia which has an implementation in python which I don't understand.
https://en.wikipedia.org/wiki/Cycle_sort
What do the lines like this do?
array[pos], item = item, array[pos]
Here's an interactive Python session which answers your question (in short: the array[pos]
gets swapped with the item
):
In [1]: array = [1,2,3]
In [2]: pos = 0
In [3]: item = 4
In [4]: array[pos], item = item, array[pos]
In [5]: array
Out[5]: [4, 2, 3]
In [6]: item
Out[6]: 1