全排列quickperm算法的分析

2013-01-01 00:28:53 +08:00
 hit9
算法的网址:quickperm.org

算法的描述:


>The Counting QuickPerm Algorithm:

let a[] represent an arbitrary list of objects to permute
let N equal the length of a[]
create an integer array p[] of size N to control the iteration
initialize p[0] to 0, p[1] to 0, p[2] to 0, ..., and p[N-1] to 0
initialize index variable i to 1
while (i < N) do {
if (p[i] < i) then {
if i is odd, then let j = p[i] otherwise let j = 0
swap(a[j], a[i])
increment p[i] by 1
let i = 1 (reset i to 1)
} // end if
else { // (p[i] equals i)
let p[i] = 0 (reset p[i] to 0)
increment i by 1
} // end else (p[i] equals i)
} // end while (i < N)


我看不懂官网的解释,求助下,谢谢。
2907 次点击
所在节点    问与答
1 条回复
yyai3
2013-01-01 10:51:59 +08:00
只能读懂大概的思路~~感觉和TAOCP里面的Algorithm L很像

例如{1,2,3,4,5},i=2时,{4,5}不动, 列出{1,2,3}的全排列,然后i=3,{5}不动,列出{1,2,3,4}的全排列。

数组p[i]就是为了做局部全排列的,但是不清楚是如何标识的。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/56310

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX