# Heap’s Algorithm for generating permutations

Heap’s algorithm is used to generate all permutations of n objects. The idea is to generate each permutation from the previous permutation by choosing a pair of elements to interchange, without disturbing the other n-2 elements.
Following is the illustration of generating all the permutations of n given numbers.

Example:

```Input: 1 2 3
Output: 1 2 3
2 1 3
3 1 2
1 3 2
2 3 1
3 2 1
```

Algorithm:

1. The algorithm generates (n-1)! permutations of the first n-1 elements, adjoining the last element to each of these. This will generate all of the permutations that end with the last element.
2. If n is odd, swap the first and last element and if n is even, then swap the ith element (i is the counter starting from 0) and the last element and repeat the above algorithm till i is less than n.
3. In each iteration, the algorithm will produce all the permutations that end with the current last element.

Implementation:

## C++

 `          `  `// C++ program to print all permutations using ` `// Heap's algorithm ` `#include ` `using` `namespace` `std; ` ` `  `//Prints the array ` `void` `printArr(``int` `a[],``int` `n) ` `{ ` `    ``for` `(``int` `i=0; i

## Java

 `// Java program to print all permutations using ` `// Heap's algorithm ` `class` `HeapAlgo ` `{ ` `    ``//Prints the array ` `    ``void` `printArr(``int` `a[], ``int` `n) ` `    ``{ ` `        ``for` `(``int` `i=``0``; i

## Python3

 `# Python program to print all permutations using ` `# Heap's algorithm ` ` `  `#Prints the array ` `def` `printArr(a, n): ` `    ``for` `i ``in` `range``(n): ` `        ``print``(a[i],end``=``" "``) ` `    ``print``() ` ` `  `# Generating permutation using Heap Algorithm ` `def` `heapPermutation(a, size, n): ` `     `  `    ``# if size becomes 1 then prints the obtained ` `    ``# permutation ` `    ``if` `(size ``=``=` `1``): ` `        ``printArr(a, n) ` `        ``return` ` `  `    ``for` `i ``in` `range``(size): ` `        ``heapPermutation(a,size``-``1``,n); ` ` `  `        ``# if size is odd, swap first and last ` `        ``# element ` `        ``# else If size is even, swap ith and last element ` `        ``if` `size&``1``: ` `            ``a[``0``], a[size``-``1``] ``=` `a[size``-``1``],a[``0``] ` `        ``else``: ` `            ``a[i], a[size``-``1``] ``=` `a[size``-``1``],a[i] ` `         `  ` `  `# Driver code ` `a ``=` `[``1``, ``2``, ``3``] ` `n ``=` `len``(a) ` `heapPermutation(a, n, n) ` ` `  `# This code is contributed by ankush_953 `

## C#

 `// C# program to print all permutations using ` `// Heap's algorithm ` `using` `System; ` `                     `  ` `  `public` `class` `GFG { ` `//Prints the array ` `    ``static` `void` `printArr(``int` `[]a, ``int` `n) ` `    ``{ ` `        ``for` `(``int` `i=0; i

Output:

```1 2 3
2 1 3
3 1 2
1 3 2
2 3 1
3 2 1
```

This article is contributed by Rahul Agrawal .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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up