Iterative approach to print all permutations of an Array | Java implementation of the approach ; The input array for permutation ; Index array to store indexes of input array ; The index of the first " increase " in the Index array which is the smallest i such that Indexes [ i ] < Indexes [ i + 1 ] ; Constructor ; Initialize and output the first permutation ; Allocate memory for Indexes array ; Initialize the values in Index array from 0 to n - 1 ; Set the Increase to 0 since Indexes [ 0 ] = 0 < Indexes [ 1 ] = 1 ; Output the first permutation ; Function that returns true if it is possible to generate the next permutation ; When Increase is in the end of the array , it is not possible to have next one ; Output the next permutation ; Increase is at the very beginning ; Swap Index [ 0 ] and Index [ 1 ] ; Update Increase ; Value at Indexes [ Increase + 1 ] is greater than Indexes [ 0 ] no need for binary search , just swap Indexes [ Increase + 1 ] and Indexes [ 0 ] ; Binary search to find the greatest value which is less than Indexes [ Increase + 1 ] ; Swap ; Invert 0 to Increase ; Reset Increase ; Function to output the input array ; Indexes of the input array are at the Indexes array ; Swap two values in the Indexes array ; Driver code
class AllPermutation { private final int Arr [ ] ; private int Indexes [ ] ; private int Increase ; public AllPermutation ( int arr [ ] ) { this . Arr = arr ; this . Increase = - 1 ; this . Indexes = new int [ this . Arr . length ] ; } public void GetFirst ( ) { this . Indexes = new int [ this . Arr . length ] ; for ( int i = 0 ; i < Indexes . length ; ++ i ) { this . Indexes [ i ] = i ; } this . Increase = 0 ; this . Output ( ) ; } public boolean HasNext ( ) { return this . Increase != ( this . Indexes . length - 1 ) ; } public void GetNext ( ) { if ( this . Increase == 0 ) { this . Swap ( this . Increase , this . Increase + 1 ) ; this . Increase += 1 ; while ( this . Increase < this . Indexes . length - 1 && this . Indexes [ this . Increase ] > this . Indexes [ this . Increase + 1 ] ) { ++ this . Increase ; } } else { if ( this . Indexes [ this . Increase + 1 ] > this . Indexes [ 0 ] ) { this . Swap ( this . Increase + 1 , 0 ) ; } else { int start = 0 ; int end = this . Increase ; int mid = ( start + end ) / 2 ; int tVal = this . Indexes [ this . Increase + 1 ] ; while ( ! ( this . Indexes [ mid ] < tVal && this . Indexes [ mid - 1 ] > tVal ) ) { if ( this . Indexes [ mid ] < tVal ) { end = mid - 1 ; } else { start = mid + 1 ; } mid = ( start + end ) / 2 ; } this . Swap ( this . Increase + 1 , mid ) ; } for ( int i = 0 ; i <= this . Increase / 2 ; ++ i ) { this . Swap ( i , this . Increase - i ) ; } this . Increase = 0 ; } this . Output ( ) ; } private void Output ( ) { for ( int i = 0 ; i < this . Indexes . length ; ++ i ) { System . out . print ( this . Arr [ this . Indexes [ i ] ] ) ; System . out . print ( " ▁ " ) ; } System . out . println ( ) ; } private void Swap ( int p , int q ) { int tmp = this . Indexes [ p ] ; this . Indexes [ p ] = this . Indexes [ q ] ; this . Indexes [ q ] = tmp ; } } class AppDriver { public static void main ( String args [ ] ) { int [ ] arr = { 0 , 1 , 2 } ; AllPermutation perm = new AllPermutation ( arr ) ; perm . GetFirst ( ) ; while ( perm . HasNext ( ) ) { perm . GetNext ( ) ; } } }