Reputation: 231
Here'a snippet of Python code:
while i > 1:
i = i - 1
j = randrange(i) # 0 <= j <= i-1
items[j], items[i] = items[i], items[j]
return
I want to use this algorithm in my Pascal project. The first 3 lines are absolutely clear to me, but what does this program do in the fourth line of code? Does it assign the random value kept on the j
to the i
th slot in the array, and then moves to the left (keeping the "used" values on the right)?
Upvotes: 0
Views: 55
Reputation: 671
Python has a neat syntax for swapping variables:
a,b = b,a
Now a
has the value of b
and vice-versa. The fourth line of your program does exactly this.
Upvotes: 1