Suppose that instead of swapping elementOf course, this is a popular problem and there are tons of posts and papers written on it. Here's one from Coding HorrorA[i] A[i] with a random element from the subarrayA[i…n] A[i…n], we swapped it with a random element from anywhere in the array:
Does this code produce a uniform random permutation? Why or why not?PERMUTE-WITH-ALL(A) n = A.length for i = 1 to n swap A[i] with A[RANDOM(1,n)]
No comments:
Post a Comment