cpp/algorithm/prev permutation

Transforms the range into the previous  from the set of all permutations that are lexicographically ordered with respect to  or. Returns if such permutation exists, otherwise transforms the range into the last permutation (as if by ) and returns.

Return value
if the new permutation precedes the old in lexicographical order. if the first permutation was reached and the range was reset to the last permutation.

Exceptions
Any exceptions thrown from iterator operations or the element swap.

Complexity
At most swaps. Averaged over the entire sequence of permutations, typical implementations use about 3 comparisons and 1.5 swaps per call.