Permutations: Difference between revisions

Content added Content deleted
Line 3,006: Line 3,006:
(2, 1, 0)</lang>
(2, 1, 0)</lang>


== Iterative implementation ==
=== Iterative implementation ===


Given a permutation, one can easily compute the ''next'' permutation in some ordre, for example lexicographic order, here. Then to get all permutations, it's enough to start from [0, 1, ... n-1], and store the next permutation until [n-1, n-2, ... 0], which is the last in lexicographic order.
Given a permutation, one can easily compute the ''next'' permutation in some order, for example lexicographic order, here. Then to get all permutations, it's enough to start from [0, 1, ... n-1], and store the next permutation until [n-1, n-2, ... 0], which is the last in lexicographic order.


<lang python>def nextperm(a):
<lang python>def nextperm(a):