Code:
#include iostream
#include iomanip
using namespace std;
// A function to print the permutation.
void print(const int *v, const int size)
{
int i;
// Print the array if it is non-empty.
if (v != 0)
{
for ( i = 0; i < size; i++)
{
cout<
}
cout<<"\n";
}
}
// A function implementing Alexander Bogomolyn algorithm.
void AlexanderBogomolyn(int *Value, int N, int k)
{
static int level = -1;
int i;
// Assign level to zero at start.
level = level+1;
Value[k] = level;
if (level == N)
print(Value, N);
else
for (i = 0; i < N; i++)
// Assign values to the array if it is zero.
if (Value[i] == 0)
AlexanderBogomolyn(Value, N, i);
// Decrement the level after all possible permutation after that level.
level = level-1;
Value[k] = 0;
}
int main()
{
int i, N, count = 1;
cout<<"Enter the N value to permute first N natural numbers: ";
cin>>N;
int Value[N];
for (i = 0; i < N; i++)
{
Value[i] = 0;
count *= (i+1);
}
// Print the permutation's count.
cout<<"\nThe number of permutations possible is: "<
// Print the permutation.
cout<<"\n\nPermutation using Alexander Bogomolyn's algorithm: \n";
AlexanderBogomolyn(Value, N, 0);
return 0;
}
Output:
Enter the N value to permute first N natural numbers: 3
The number of permutations possible is: 6
Permutation using Alexander Bogomolyn's algorithm:
1 2 3
1 3 2
2 1 3
3 1 2
2 3 1
3 2 1
Case 2:
Enter the N value to permute first N natural numbers: 4
The number of permutations possible is: 24
Permutation using Alexander Bogomolyn's algorithm:
1 2 3 4
1 2 4 3
1 3 2 4
1 4 2 3
1 3 4 2
1 4 3 2
2 1 3 4
2 1 4 3
3 1 2 4
4 1 2 3
3 1 4 2
4 1 3 2
2 3 1 4
2 4 1 3
3 2 1 4
4 2 1 3
3 4 1 2
4 3 1 2
2 3 4 1
2 4 3 1
3 2 4 1
4 2 3 1
3 4 2 1
4 3 2 1
More C++ Programs: