Is there any algorithm to generate all permutations of the integers 1 to n [ Hint: The set of permutations of the integers 1 to n can be obtained from the set of permutations of the integers 1 to n-1 by inserting n in each possible positions of each permutation ]
Remember to vote up questions/answers you find interesting or helpful (requires 15 reputation points)
|
-1
|
||||||||||||||||||||||||
|
closed as off topic by Sergei Ivanov, Victor Protsak, Andrew Stacey, Charles Matthews, José Figueroa-O'Farrill Sep 6 2010 at 11:09 |
|
4
|
Knuth's The Art of Computer Programming, Volume 4, Fascicle 2: Generating all Tuples and Permutations gives efficient (non-recursive) solutions to this and many other combinatorial enumeration problems. Section 7.2.1.2 contains 36 pages of material devoted to precisely the question you ask. |
||||||
|
You can accept an answer to one of your own questions by clicking the check mark next to it. This awards 15 reputation points to the person who answered and 2 reputation points to you.
|
1
|
Hint: If you understand recursion, you just answered your own question. You may also want to look at the C++ implementation of next_permutation in the STL. I was going to explain it, but http://wordaligned.org/articles/next-permutation does a fantastic job. |
|||
|
|

