![]() |
VOOZH | about |
Alexander Bogomolny's algorithm is used to permute first N natural numbers.
Given the value of N, we have to output all the permutations of numbers from 1 to N.
Examples:
Input : 2 Output : 1 2 2 1 Input : 3 Output : 1 2 3 1 3 2 2 1 3 3 1 2 2 3 1 3 2 1
The idea is to maintain an array to store the current permutation. A static integer level variable is used to define these permutations.
1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1
Time Complexity: O(N*N!), where N is the given integer.
Auxiliary Space: O(N*N!), for storing all the permutations of the first N natural numbers.