Exercises Videos Notes Multiple-choice questions

Related books

Algorithms Algorithms Concentration of Measure for the Analysis of Randomized Algorithms Randomized Algorithms Probability and Computing: Randomized Algorithms and Probabilistic Analysis Suggest a Book
2

Linear time shuffling

Level: unknown
 

posted Jul 21 '12

kintali gravatar image Shiva Kintali flag of United States
691 1 6 22
http://www.cs.princeton.e...

updated Jul 21 '12

Consider the following algorithm to shuffle an array $A$ of $n$ elements (with indices from $0$ to $n-1$ :

for $i = 0$ to $n - 1$

Let j be a random integer between 0 and i

exchange A[j] and A[i]

Prove that the above algorithm shuffles the array $A$ uniformly i.e., each of the $n!$ permutations are equally likely.

Source: folklore
Printable version LaTeX source
delete flag offensive retag edit

Comments

I would replace the first line with "for i = 0 to n-1". Yes, 0.

JeffE (Jul 21 '12)edit

@JeffE Thanks for pointing that out.

Shiva Kintali (Jul 21 '12)edit

Stats

Posted: Jul 21 '12

Seen: 103 times

Last updated: Jul 21 '12