VOOZH about

URL: https://www.geeksforgeeks.org/dsa/alexander-bogomolnys-unordered-permutation-algorithm/

⇱ Alexander Bogomolny’s UnOrdered Permutation Algorithm - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Alexander Bogomolny’s UnOrdered Permutation Algorithm

Last Updated : 6 Sep, 2022

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. It initializes the value of the current level and permutes the remaining values to higher levels.
  2. As the assigning action of the values reaches the highest level, it prints the permutation obtained.
  3. This approach is recursively implemented to obtain all possible permutations.


 


 


Output
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.


Comment
Article Tags: