北航C++教程.pptVIP

  • 13
  • 0
  • 约1.67万字
  • 约 35页
  • 2016-08-15 发布于河南
  • 举报
北航C教程

* * * * * * * * * 5.3.2 Randomly permuting an array (2) RANDOMIZE-IN-PLACE RANDOMIZE-IN-PLACE(A, n) for(i=1; i=n; i++) swap(A[i], A[RANDOM(i, n)] Lemma RANDOMIZE-IN-PLACE computes a uniform random permutation. Proof Loop invariant: Pr{A[1 .. i-1] contains each (i-1)-permutation} = (n-i+1)!/n! . Maintenance: … … i-permutation R=x1, x2, … , xi = x1, x2, … , xi ∪ xi = R’ ∪ xi . Let E1 be the event that the algorithm actually puts R’ into A[1 .. i-1] . By the loop invariant, Pr{E1}=(n-i+1)!/n! . Let E2 be the event that the ith iteration puts xi into A[i]. We get the i-Permutation R in

文档评论(0)

1亿VIP精品文档

相关文档