human
Print all permutations in sorted ( lexicographic ) order | An optimized version that uses reverse instead of sort for finding the next permutation A utility function to reverse a string str [ l . . h ].
.
Print all permutations of str in sorted order.
Get size of string.
Sort the string in increasing order.
Print permutations one by one.
Print this permutation.
Find the rightmost character which is smaller than its next character . Let us call it ' first ▁ char '.
If there is no such character , all are sorted in decreasing order , means we just printed the last permutation and we are done ..
Find the ceil of ' first ▁ char ' in right of first character . Ceil of a character is the smallest character greater than it.
Swap first and second characters.
Reverse the string on right of 'first char"
Solution in Python:
assistant
#Here is code to print all permutations in sorted ( lexicographic ) order and the other steps required.
 def reverse ( str , l , h ) :
	while ( l < h ) :
		str [ l ] , str [ h ] = str [ h ] , str [ l ]
		l += 1
		h -= 1
	return str
def findCeil ( str , c , k , n ) :
	ans = - 1
	val = c
	for i in range ( k , n + 1 ) :
		if str [ i ] > c and str [ i ] < val :
			val = str [ i ]
			ans = i
	return ans
def sortedPermutations ( str ) :
	size = len ( str )
	str = ' ' . join ( sorted ( str ) )
	isFinished = False
	while ( not isFinished ) :
		print ( str )
		for i in range ( size - 2 , - 1 , - 1 ) :
			if ( str [ i ] < str [ i + 1 ] ) :
				break
		if ( i == - 1 ) :
			isFinished = True
		else :
			ceilIndex = findCeil ( str , str [ i ] , i + 1 , size - 1 )
			str [ i ] , str [ ceilIndex ] = str [ ceilIndex ] , str [ i ]
			str = reverse ( str , i + 1 , size - 1 )