It’s been a long time I have done any activity in this blog. I was going through some old stuffs and thought to post something. Today I will post a generic implementation for Fisher-Yates Shuffle.
Although you can get the Fisher-Yates algorithm from wiki, still I am briefly explaining it.
Let us assume that there is a array arr of length n. We need to find a uniformly random permutation of the elements of the array. One of the variations of the algorithm is as follows.
arr is an array of length n, where indexing starts from 0
i=n-1
while i>0
{
r = generate a random number between 0 and i (both inclusive)
swap the array elements arr[r] and arr[i]
i = i - 1
}
arr is now uniformly permuted
