Javatpoint Logo
Javatpoint Logo

Different Ways to Generate Permutations of an Array in C++

Permutations are like the magic wand of combinatorics, allowing us to explore how elements can be rearranged within an array. Knowing how to generate all permutations of an array is a useful skill, whether we're a coder, a math nerd, or someone trying to solve a challenging puzzle. In this article, we will learn many methods for generating array permutations.

What does Permutations of an Array Means?

Suppose we wish to create a word with the three letters A, B, and C. They can be arranged in various configurations, known as permutations. The options are as follows: ABC, ACB, BAC, BCA, CBA, and CAB. Order matters in permutations, which is what this is all about.

The crucial thing is that, occasionally, individuals refer to permutations as i instead of combinations. However, there is a distinction in mathematics. Combinations are made up of items chosen without regard to their sequence. For instance, if we roll two dice and look at the sum, the outcome is the same whether we roll a three and a four or a four and a three. In that combination, the order is irrelevant.

How Many Permutations Can Be Generated?

The size of the set or array determines how many permutations can be made from a given collection of components. The number of permutations of a set of "n" distinct elements taken "r" at a time can be found in combinatory using the following formula:

Different Ways to Generate Permutations of an Array in C++

Where:

  • P(n,r) represents the number of permutations.
  • n is the total number of distinct elements.
  • r is the number of elements taken at a time.
  • n! (read as "n factorial") represents the product of all positive integers from 1 to n.

The formula reduces if we wish to produce every permutation of a given set (where "r" equals "n").

Different Ways to Generate Permutations of an Array in C++

In other words, we can create "n!" (n factorial) permutations for a set of "n" different components. As "n" increases, the number of permutations increases quickly. For instance, there are 3! = 6 permutations if we have 3 items, 4! = 24 permutations if we have 4 elements, and so on.

1. Using std::next_permutation from <algorithm>:

In this method, the <algorithm> header in C++ provides a convenient function for generating permutations.

Example:

Output:

Different Ways to Generate Permutations of an Array in C++

Explanation:

In this example, the next lexicographically greater permutation is generated by std::next_permutation, which also returns false when there are no more permutations, which is the basis for this method.

2. Recursive Backtracking:

We can implement a recursive backtracking algorithm to generate permutations.

Example:

Output:

Different Ways to Generate Permutations of an Array in C++

Explanation:

In this example, this approach uses recursive calls and backtracking to generate all possible permutations.

3. Heap's Algorithm:

Heap's Algorithm is an iterative algorithm to generate permutations. It is based on swapping elements.

Example:

Output:

Different Ways to Generate Permutations of an Array in C++

Explanation:

In this example, heap's Algorithm generates permutations non-reclusively using iterative loops and swaps.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA