Get the next permutation in lexicographic permutation order.

The next permutation is obtained from the selected permutation by swapping values *at only two positions*. Starting with the identity permutation and repeatedly applying this function will iterate through all possible permutations. If no further permutation is available, i.e. the selected permutation is at the lexicographic end position (n, n-1, ..., 3, 2, 1), the current permutation will not change anymore.

###
Examples

If we start with (1,2,3,4) successively applying **Next** will generate the following sequence (1,2,4,3), (1,3,2,4), (1,3,4,2), (1,4,2,3), (1,4,3,2), etc.

### Links to this page

© djmw, January 31, 2014