Before I read about Fisher-Yates, this is the algorithm I came up with:
def sort(arr):
for i in range(len(arr)):
swap(arr, i, rand.randint(0, len(arr) - 1))
From my understanding, the only difference between this and Fisher-Yates is that instead of:
swap(arr, i, rand.randint(0, len(arr) - 1))
I should write:
swap(arr, i, rand.randint(i, len(arr) - 1))
Could someone explain how the first algorithm is incorrect? (ie. does not produce a random shuffle).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…