How would you calculate all possible permutations of 0 through N iteratively?

Posted by Bob Aman on Stack Overflow See other posts from Stack Overflow or by Bob Aman
Published on 2010-03-06T01:03:02Z Indexed on 2010/03/08 17:21 UTC
Read the original article Hit count: 183

I need to calculate permutations iteratively. The method signature looks like:

int[][] permute(int n)

For n = 3 for example, the return value would be:

[[0,1,2],
 [0,2,1],
 [1,0,2],
 [1,2,0],
 [2,0,1],
 [2,1,0]]

How would you go about doing this iteratively in the most efficient way possible? I can do this recursively, but I'm interested in seeing lots of alternate ways to doing it iteratively.

© Stack Overflow or respective owner

Related posts about language-agnostic

Related posts about math