def lexicographically_next_permutation(a):
    """
    Generates the lexicographically next permutation.
    Input: a permutation, called "a". This method modifies
    "a" in place. Returns True if we could generate a next
    permutation. Returns False if it was the last permutation
    lexicographically.
    """
i = len(a) - 2
while not (i < 0 or a[i] < a[i + 1]):
    i -= 1
if i < 0:
    return False
j = len(a) - 1
while not a[j] > a[i]:
    j -= 1
a[i], a[j] = a[j], a[i]
a[i + 1:] = reversed(a[i + 1:])
return True
Реализация Python для next_permutation в STL