Module jabba_lib::jpermutation
source · [−]Expand description
permutation
Generate the lexicographically next permutation of a sequence of elements.
Pseudo-code:
- Find the largest index
k
such thata[k] < a[k + 1]
. If no such index exists, the permutation is the last permutation. - Find the largest index
l
such thata[k] < a[l]
. Sincek + 1
is such an index,l
is well-defined and satisfiesk < l
. - Swap
a[k]
witha[l]
. - Reverse the sequence from
a[k + 1]
up to end including the final element.
Functions
Generates the lexicographically next permutation of an array / vector.