According to wikipedia and also the implementation of Java standard library, shuffling https://en.wikipedia.org/wiki/Fisher–Yates_shuffle (Fisher Yates Shuffling) works like this:
Algo A:
-- To shuffle an array a of n elements (indices 0..n-1):
for i from n−1 downto 1 do
j ← random integer such that 0 ≤ j ≤ i
exchange a[j] and a[i]
or equivalently
Algo B:
-- To shuffle an array a of n elements (indices 0..n-1):
for i from 0 to n−2 do
j ← random integer such that i ≤ j < n
exchange a[i] and a[j]
My question is for the one below (Algo C):
Algo C:
-- To shuffle an array a of n elements (indices 0..n-1):
for i from 1 to n−1 do
j ← random integer such that 0 ≤ j ≤ i
exchange a[i] and a[j]
Algo A and Algo B are just identical. But Algo C is different from Algo A and Algo B (in fact Algo C is Algo A executed in reverse direction)
Is Algo C correct? I am very confused. I did some chi-squared test using contingency table, and it appears that this gives apparently correct uniform order.
My question is whether Algo C is correct? If correct, why is it seen almost no where? Why F-Y shuffle is everywhere presented in one same direction.
Focusing on why it isn't seen more (as the other answer already shows that it is correct).
The variants are useful as optimizations in various cases:
Those additional uses mean that Algo B and A will be coded more, and used even in other cases.