I found out the knuth shuffle was done from the end to the beginning, such as
from random import randrange
def knuth_shuffle(x):
for i in range(len(x)-1, 0, -1):
j = randrange(i + 1)
x[i], x[j] = x[j], x[i]
return x
However, I was thinking about why we cannot use it from the beginning to the end. Like this:
from random import randrange
def knuth_shuffle(x):
for i in range(0, len(x), 1):
j = randrange(i, len(x))
x[i], x[j] = x[j], x[i]
return x
I found out the running time of the second function was always longer than the first one. Anybody has some clues of this?