ウィキペディアから:
辞書式順序生成
0 ≤ k < n! のすべての数値 k に対して、次のアルゴリズムは、初期シーケンス sj、j = 1、...、n の対応する辞書編集順列を生成します。
function permutation(k, s) { var int n:= length(s); factorial:= 1; for j= 2 to n- 1 { // compute (n- 1)! factorial:= factorial* j; } for j= 1 to n- 1 { tempj:= (k/ factorial) mod (n+ 1- j); temps:= s[j+ tempj] for i= j+ tempj to j+ 1 step -1 { s[i]:= s[i- 1]; // shift the chain right } s[j]:= temps; factorial:= factorial/ (n- j); } return s; }
この背後にあるロジックは何ですか? それはどのように機能しますか??