MathOverflow is a question and answer site for professional mathematicians. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 ]

share|cite|improve this question

closed as off topic by Sergei Ivanov, Victor Protsak, Loop Space, Charles Matthews, José Figueroa-O'Farrill Sep 6 '10 at 11:09

Questions on MathOverflow are expected to relate to research level mathematics within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

have you checked… also, googoling gives plenty of results. In particular, nice explanations in the archives of the wiki Ref Desk (plug "generating permutations") – Pietro Majer Sep 6 '10 at 9:03
The phrase "That hint they only provided" makes this question sound like this is some kind of exercise or homework assignment. Is this the case? – Yemon Choi Sep 6 '10 at 9:18
The section "Systematic generation of all permutations" in the Wikipedia article on permutations gives a complete step-by-step algorithm. I doubt anyone here will give a more detailed answer than that (and if you want an actual computer program, you should ask elsewhere). I'm voting to close. – Sergei Ivanov Sep 6 '10 at 9:23
This question is not appropriate for MO. You are asking us to solve a basic programming exercise. – Andrej Bauer Sep 6 '10 at 9:24
MathOverflow is not for homework help. Please read – Tsuyoshi Ito Sep 6 '10 at 10:56

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 contains 36 pages of material devoted to precisely the question you ask.

share|cite|improve this answer
@AVS: This is off-topic, but what became of the model behind you on your site? – Eric Tressler Sep 6 '10 at 9:35
@Eric Tressler: 'twas but an ephemeral shadow of the 4-dimensional object we were chasing, and such things are not meant to last. But I trust that its components have since been used to build even bigger and better things. – AVS Sep 6 '10 at 10:10

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 does a fantastic job.

share|cite|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.