Iterative approach to print all permutations of an Array | C # 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
using System ; namespace Permutation { class AllPermutation < T > { private readonly T [ ] Arr ; private int [ ] Indexes ; private int Increase ; public AllPermutation ( T [ ] arr ) { this . Arr = arr ; this . Increase = - 1 ; } 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 bool 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 ) { Console . Write ( this . Arr [ this . Indexes [ i ] ] ) ; Console . Write ( " ▁ " ) ; } Console . WriteLine ( ) ; } private void Swap ( int p , int q ) { int tmp = this . Indexes [ p ] ; this . Indexes [ p ] = this . Indexes [ q ] ; this . Indexes [ q ] = tmp ; } } class AppDriver { static void Main ( ) { int [ ] arr = { 0 , 1 , 2 } ; AllPermutation < int > perm = new AllPermutation < int > ( arr ) ; perm . GetFirst ( ) ; while ( perm . HasNext ( ) ) { perm . GetNext ( ) ; } } } }