Algorithm to generate all possible permutations of a list?

Answer

Basically, for each element from left to right, you generate all the permutations of the remaining elements. You can do this recursively, (or iteratively if you like pain) until you get to the last element at which point there is only one possible order.

So, given a list: [1,2,3,4]

You just generate all permutations that start with 1, then all the permutations that start with 2, then with 3 and 4.

This effectively reduces the problem from one of finding permutations of a list of four elements to a list of three elements. Once you continue reducing to 2 and then 1 element, you have all of them.

All algorithm Questions

Ask your interview questions on algorithm

Write Your comment or Questions if you want the answers on algorithm from algorithm Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---