0

I've read this similar question How to prove correctness of a shuffle algorithm? and I understand the answer. However I still couldn't figure out how to apply that inductive proof to a similar shuffle algorithm --- inside-out shuffling algorithm, which is as follows:

  for i from 0 to n − 1 do
      j ← random integer such that 0 ≤ j ≤ i
      if j ≠ i
          a[i] ← a[j]
      a[j] ← source[i]

source is the input array and a is the output array.

Another question I have is: would it be the same if I changed the code to the following:

  for i from 0 to n − 1 do
      j ← random integer such that 0 ≤ j ≤ i
      swap(a[j], a[i])

This is similar to the knuth algorithm except that it chooses a random element in the first i elements instead of last n-i elements. I have the feeling that they shouldn't both work, but I'm not sure.

eaglesky
  • 101

0 Answers0