cpp/algorithm/next permutation

Permutes the range into the next, where the set of all permutations is ordered lexicographically with respect to  or. Returns if such a "next permutation" exists; otherwise transforms the range into the lexicographically first permutation (as if by ) and returns.

Return value
if the new permutation is lexicographically greater than the old. if the last permutation was reached and the range was reset to the first permutation.

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

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