NationStates Jolt Archive


Any clever people in the house?

Shadowstorm Imperium
19-05-2005, 19:47
I am wondering how I could program a java method to return all permutations of an array. The method would look like this:

public static int[][] allperm(int[] a)

so, for example, if I put in:

{1, 2, 3}

I would get out something like:

{ {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1} }
Myrmidonisia
19-05-2005, 20:00
I am wondering how I could program a java method to return all permutations of an array. The method would look like this:

public static int[][] allperm(int[] a)

so, for example, if I put in:

{1, 2, 3}

I would get out something like:

{ {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1} }
When I start with algorithm design, I like to look at "Numerical Recipies" for ideas. Chances are good that they may already have one coded in C or FORTRAN. I think they have a website with all of their code in pdf.

They do, it's at www.nr.com

You might be able to make a matrix out of the array, then apply some pivoting algorithms.
Whispering Legs
19-05-2005, 20:11
Normally, I get paid to do people's homework.

I'm sure there's a book on Algorithms at the library.

http://www.amazon.com/exec/obidos/ASIN/0262032937/qid=1116529839/sr=2-1/ref=pd_bbs_b_2_1/103-2246233-1534214

That's a good book.
Shadowstorm Imperium
19-05-2005, 20:15
Normally, I get paid to do people's homework.

It's not homework, I'm just experimenting with programming, but this particular thing I wanted to do seems a bit too hard for me to work out by guesswork.
Myrmidonisia
19-05-2005, 20:33
It's not homework, I'm just experimenting with programming, but this particular thing I wanted to do seems a bit too hard for me to work out by guesswork.
I think you need to work on pivoting the elements in the input array. There should be a systematic progression to it. First swap 1 & 2. Record the result, then swap 1 & 3, and so on.

Or you could type permutation algorithm into google and see what happens.