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).
From Wikipedia:
Essentially, you are introducing a subtle bias into the shuffle, which will cause some permutations to crop up a bit more often than others. It's often not very noticeable, but it could make some sensitive applications (e.g. Monte Carlo simulations on permutations) fail to produce accurate answers.