# Iterative approach to print all permutations of an Array

Given an array arr[] of size N, the task is to generate and print all permutations of the given array.

Examples:

Input: arr[] = {1, 2}
Output:
1 2
2 1

Input: {0, 1, 2}
Output:
0 1 2
1 0 2
0 2 1
2 0 1
1 2 0
2 1 0

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The recursive methods to solve the above problems are discussed here and here. In this post, an iterative method to output all permutations for a given array will be discussed.
The iterative method acts as a state machine. When the machine is called, it outputs a permutation and move to the next one.

To begin, we need an integer array Indexes to store all the indexes of the input array, and values in array Indexes are initialized to be 0 to n – 1. What we need to do is to permute the Indexes array.

During the iteration, we find the smallest index Increase in the Indexes array such that Indexes[Increase] < Indexes[Increase + 1], which is the first “value increase”. Then, we have Indexes[0] > Indexes[1] > Indexes[2] > … > Indexes[Increase], which is a tract of decreasing values from index[0]. The next steps will be:

1. Find the index mid such that Indexes[mid] is the greatest with the constraints that 0 ≤ mid ≤ Increase and Indexes[mid] < Indexes[Increase + 1]; since array Indexes is reversely sorted from 0 to Increase, this step can use binary search.
2. Swap Indexes[Increase + 1] and Indexes[mid].
3. Reverse Indexes[0] to Indexes[Increase].

When the values in Indexes become n – 1 to 0, there is no “value increase”, and the algorithm terminates.

To output the combination, we loop through the index array and the values of the integer array are the indexes of the input array.

The following image illustrates the iteration in the algorithm.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `template` `<``typename` `T> ` `class` `AllPermutation { ` `private``: ` `    ``// The input array for permutation ` `    ``const` `T* Arr; ` ` `  `    ``// Length of the input array ` `    ``const` `int` `Length; ` ` `  `    ``// Index array to store indexes of input array ` `    ``int``* Indexes; ` ` `  `    ``// The index of the first "increase" ` `    ``// in the Index array which is the smallest ` `    ``// i such that Indexes[i] < Indexes[i + 1] ` `    ``int` `Increase; ` ` `  `public``: ` `    ``// Constructor ` `    ``AllPermutation(T* arr, ``int` `length) ` `        ``: Arr(arr), Length(length) ` `    ``{ ` `        ``this``->Indexes = nullptr; ` `        ``this``->Increase = -1; ` `    ``} ` ` `  `    ``// Destructor ` `    ``~AllPermutation() ` `    ``{ ` `        ``if` `(``this``->Indexes != nullptr) { ` `            ``delete``[] ``this``->Indexes; ` `        ``} ` `    ``} ` ` `  `    ``// Initialize and output ` `    ``// the first permutation ` `    ``void` `GetFirst() ` `    ``{ ` ` `  `        ``// Allocate memory for Indexes array ` `        ``this``->Indexes = ``new` `int``[``this``->Length]; ` ` `  `        ``// Initialize the values in Index array ` `        ``// from 0 to n - 1 ` `        ``for` `(``int` `i = 0; i < ``this``->Length; ++i) { ` `            ``this``->Indexes[i] = i; ` `        ``} ` ` `  `        ``// Set the Increase to 0 ` `        ``// since Indexes[0] = 0 < Indexes[1] = 1 ` `        ``this``->Increase = 0; ` ` `  `        ``// Output the first permutation ` `        ``this``->Output(); ` `    ``} ` ` `  `    ``// Function that returns true if it is ` `    ``// possible to generate the next permutation ` `    ``bool` `HasNext() ` `    ``{ ` ` `  `        ``// When Increase is in the end of the array, ` `        ``// it is not possible to have next one ` `        ``return` `this``->Increase != (``this``->Length - 1); ` `    ``} ` ` `  `    ``// Output the next permutation ` `    ``void` `GetNext() ` `    ``{ ` ` `  `        ``// Increase is at the very beginning ` `        ``if` `(``this``->Increase == 0) { ` ` `  `            ``// Swap Index[0] and Index[1] ` `            ``this``->Swap(``this``->Increase, ``this``->Increase + 1); ` ` `  `            ``// Update Increase ` `            ``this``->Increase += 1; ` `            ``while` `(``this``->Increase < ``this``->Length - 1 ` `                   ``&& ``this``->Indexes[``this``->Increase] ` `                          ``> ``this``->Indexes[``this``->Increase + 1]) { ` `                ``++``this``->Increase; ` `            ``} ` `        ``} ` `        ``else` `{ ` ` `  `            ``// Value at Indexes[Increase + 1] is greater than Indexes[0] ` `            ``// no need for binary search, ` `            ``// just swap Indexes[Increase + 1] and Indexes[0] ` `            ``if` `(``this``->Indexes[``this``->Increase + 1] > ``this``->Indexes[0]) { ` `                ``this``->Swap(``this``->Increase + 1, 0); ` `            ``} ` `            ``else` `{ ` ` `  `                ``// Binary search to find the greatest value ` `                ``// which is less than Indexes[Increase + 1] ` `                ``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; ` `                ``} ` ` `  `                ``// Swap ` `                ``this``->Swap(``this``->Increase + 1, mid); ` `            ``} ` ` `  `            ``// Invert 0 to Increase ` `            ``for` `(``int` `i = 0; i <= ``this``->Increase / 2; ++i) { ` `                ``this``->Swap(i, ``this``->Increase - i); ` `            ``} ` ` `  `            ``// Reset Increase ` `            ``this``->Increase = 0; ` `        ``} ` `        ``this``->Output(); ` `    ``} ` ` `  `private``: ` `    ``// Function to output the input array ` `    ``void` `Output() ` `    ``{ ` `        ``for` `(``int` `i = 0; i < ``this``->Length; ++i) { ` ` `  `            ``// Indexes of the input array ` `            ``// are at the Indexes array ` `            ``cout << (``this``->Arr[``this``->Indexes[i]]) << ``" "``; ` `        ``} ` `        ``cout << endl; ` `    ``} ` ` `  `    ``// Swap two values in the Indexes array ` `    ``void` `Swap(``int` `p, ``int` `q) ` `    ``{ ` `        ``int` `tmp = ``this``->Indexes[p]; ` `        ``this``->Indexes[p] = ``this``->Indexes[q]; ` `        ``this``->Indexes[q] = tmp; ` `    ``} ` `}; ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 0, 1, 2 }; ` `    ``AllPermutation<``int``> perm(arr, ``sizeof``(arr) / ``sizeof``(``int``)); ` `    ``perm.GetFirst(); ` `    ``while` `(perm.HasNext()) { ` `        ``perm.GetNext(); ` `    ``} ` ` `  `    ``return` `0; ` `} `

## Java

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

## C#

 `// C# implementation of the approach ` `using` `System; ` `namespace` `Permutation { ` ` `  `class` `AllPermutation { ` ` `  `    ``// The input array for permutation ` `    ``private` `readonly` `T[] Arr; ` ` `  `    ``// Index array to store indexes of input array ` `    ``private` `int``[] Indexes; ` ` `  `    ``// The index of the first "increase" ` `    ``// in the Index array which is the smallest ` `    ``// i such that Indexes[i] < Indexes[i + 1] ` `    ``private` `int` `Increase; ` ` `  `    ``// Constructor ` `    ``public` `AllPermutation(T[] arr) ` `    ``{ ` `        ``this``.Arr = arr; ` `        ``this``.Increase = -1; ` `    ``} ` ` `  `    ``// Initialize and output ` `    ``// the first permutation ` `    ``public` `void` `GetFirst() ` `    ``{ ` ` `  `        ``// Allocate memory for Indexes array ` `        ``this``.Indexes = ``new` `int``[``this``.Arr.Length]; ` ` `  `        ``// Initialize the values in Index array ` `        ``// from 0 to n - 1 ` `        ``for` `(``int` `i = 0; i < Indexes.Length; ++i) { ` `            ``this``.Indexes[i] = i; ` `        ``} ` ` `  `        ``// Set the Increase to 0 ` `        ``// since Indexes[0] = 0 < Indexes[1] = 1 ` `        ``this``.Increase = 0; ` ` `  `        ``// Output the first permutation ` `        ``this``.Output(); ` `    ``} ` ` `  `    ``// Function that returns true if it is ` `    ``// possible to generate the next permutation ` `    ``public` `bool` `HasNext() ` `    ``{ ` ` `  `        ``// When Increase is in the end of the array, ` `        ``// it is not possible to have next one ` `        ``return` `this``.Increase != (``this``.Indexes.Length - 1); ` `    ``} ` ` `  `    ``// Output the next permutation ` `    ``public` `void` `GetNext() ` `    ``{ ` ` `  `        ``// Increase is at the very beginning ` `        ``if` `(``this``.Increase == 0) { ` ` `  `            ``// Swap Index[0] and Index[1] ` `            ``this``.Swap(``this``.Increase, ``this``.Increase + 1); ` ` `  `            ``// Update Increase ` `            ``this``.Increase += 1; ` `            ``while` `(``this``.Increase < ``this``.Indexes.Length - 1 ` `                   ``&& ``this``.Indexes[``this``.Increase] ` `                          ``> ``this``.Indexes[``this``.Increase + 1]) { ` `                ``++``this``.Increase; ` `            ``} ` `        ``} ` `        ``else` `{ ` ` `  `            ``// Value at Indexes[Increase + 1] is greater than Indexes[0] ` `            ``// no need for binary search, ` `            ``// just swap Indexes[Increase + 1] and Indexes[0] ` `            ``if` `(``this``.Indexes[``this``.Increase + 1] > ``this``.Indexes[0]) { ` `                ``this``.Swap(``this``.Increase + 1, 0); ` `            ``} ` `            ``else` `{ ` ` `  `                ``// Binary search to find the greatest value ` `                ``// which is less than Indexes[Increase + 1] ` `                ``int` `start = 0; ` `                ``int` `end = ``this``.Increase; ` `                ``int` `mid = (start + end) / 2; ` `                ``int` `tVal = ``this``.Indexes[``this``.Increase + 1]; ` `                ``while` `(!(``this``.Indexes[mid] tVal)) { ` `                    ``if` `(``this``.Indexes[mid] < tVal) { ` `                        ``end = mid - 1; ` `                    ``} ` `                    ``else` `{ ` `                        ``start = mid + 1; ` `                    ``} ` `                    ``mid = (start + end) / 2; ` `                ``} ` ` `  `                ``// Swap ` `                ``this``.Swap(``this``.Increase + 1, mid); ` `            ``} ` ` `  `            ``// Invert 0 to Increase ` `            ``for` `(``int` `i = 0; i <= ``this``.Increase / 2; ++i) { ` `                ``this``.Swap(i, ``this``.Increase - i); ` `            ``} ` ` `  `            ``// Reset Increase ` `            ``this``.Increase = 0; ` `        ``} ` `        ``this``.Output(); ` `    ``} ` ` `  `    ``// Function to output the input array ` `    ``private` `void` `Output() ` `    ``{ ` `        ``for` `(``int` `i = 0; i < ``this``.Indexes.Length; ++i) { ` ` `  `            ``// Indexes of the input array ` `            ``// are at the Indexes array ` `            ``Console.Write(``this``.Arr[``this``.Indexes[i]]); ` `            ``Console.Write(``" "``); ` `        ``} ` `        ``Console.WriteLine(); ` `    ``} ` ` `  `    ``// Swap two values in the Indexes array ` `    ``private` `void` `Swap(``int` `p, ``int` `q) ` `    ``{ ` `        ``int` `tmp = ``this``.Indexes[p]; ` `        ``this``.Indexes[p] = ``this``.Indexes[q]; ` `        ``this``.Indexes[q] = tmp; ` `    ``} ` `} ` ` `  `// Driver code ` `class` `AppDriver { ` `    ``static` `void` `Main() ` `    ``{ ` `        ``int``[] arr = { 0, 1, 2 }; ` `        ``AllPermutation<``int``> perm = ``new` `AllPermutation<``int``>(arr); ` `        ``perm.GetFirst(); ` `        ``while` `(perm.HasNext()) { ` `            ``perm.GetNext(); ` `        ``} ` `    ``} ` `} ` `} `

Output:

```0 1 2
1 0 2
0 2 1
2 0 1
1 2 0
2 1 0
```

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : gp6