What is Recursive Permutation in C++? [Algorithm and Source Code]

This article will describe a quick and easy algorithm that gives the full permutation for a natural number. The algorithm will be implemented by C++.

A full permutation is list of all variation for given items (usually numbers). For example, the full permutation of 3 elements are:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Also Read: C program to find factorial of any number using recursion
Also Read: C++ program to enter a number and print it into words

As we can easily calculate, the total number of iterations is n! (factorial). How do we get this number? The most simple way is to think of the number of different items we can choose for each position. For example, for the first place, we have n choices, and we have n-1 choices for the second place, and etc. Thus we have n*(n-1)*(n-2)*…1 that gives a total number n!.

So how to emulate this for the whole process? First, we can define a recursive function that ends printing one solution by checking if we have reached the end. For each iteration, we can simply swap the current item with the rest of the items and try the next position recursive. As we use a global array variable nums to keep the items, we need to swap the items back after each recursion call. The source code is pretty straight forward. Open Visual Studio, choose the ‘Console Application’ from ‘C++’ language projects.

Recursive Permutation in C++? [Algorithm and Source Code]

And paste the following source code into Visual Studio.
#include <iostream>
using namespace std;
// helper dynamic array
int *nums;
// exchange two integers
inline void swap(int &i, int &j)
{
        int t = i;
        i = j;
        j = t;
}
// full permutation
void
perm(int n, int i)
{
        if (i == n – 1) //
check if reach the end of iteration
        {
               // print out
the iteration
               for (int j =
0; j < n; j ++)
               {
                       cout
<< nums[j];
               }
               cout <<
endl;
        }
        else
        {
               for (int j =
i; j < n; j ++)
               {
                       swap(nums[i],
nums[j]); // swap
                       perm(n,
i + 1);         // recursive
                       swap(nums[i],
nums[j]); // swap it back
               }
        }
}
// main entry
int
main()
{
        int n;
        cin >> n;
        nums = new int[n]; //
declare a dynamic array
        for (int i = 0; i
< n; i ++)
        {
               // and fill it
with numbers as 1, 2, 3, … n
               nums[i] = i +
1;
        }
        perm(n, 0);
        cin >> n; //
pause
        return 0;

}
Press F5 to run the project, put a number, e.g. 4, and the program will give the full permutation of 4. The last cin >> n is the C++ easy way to pause the screen after the program terminates.

Recursive Permutation in C++? [Algorithm and Source Code]

Author’s bio:
Zhihua Lai is now a Marie Curie Experienced Researcher at University of Sheffield. At his spare time, he writes blogs and programs just for fun. He is also an ACMer where he is keen on fast and efficient algorithms. His blog can be found at http://helloacm.com/

1 thought on “What is Recursive Permutation in C++? [Algorithm and Source Code]”

  1. Hi, I'm sort of a beginner at programming. I've used VS2012, opened a new project for VC++ and select Console Application. Copy and paste the program and when I press F5, VS says me that there an error.
    error C1033: cannot open program database ''
    Could someone help me?

Leave a Comment

Your email address will not be published. Required fields are marked *