tags:

views:

102

answers:

3

For example I have array:

int a[] = new int[]{3,4,6,2,1};

I need list of all permutations such tha if one is like this, {3,2,1,4,6}, others must not be the same. I know that if the length of the array is n then there are n! possible combinations. How can this algortihm be written?

Update: thanks, but I need a pseudo code algorithm like:

    for(int i=0;i<a.length;i++){
        // code here
    }

Just algorithm. Yes, API functions are good, but it does not help me too much.

A: 

Example of an implementation (in python):

The MYYN
in java please or in c++
+2  A: 

If you're using C++, you can use std::next_permutation from algorithm header:

int a[] = {3,4,6,2,1};
int size = sizeof(a)/sizeof(a[0]);
std::sort(a, a+size);
do {
  // print a's elements
} while(std::next_permutation(a, a+size));
reko_t
+2  A: 

Here is an implementation of the Permutation in Java:

Permutation - Java

You should have a check on it!

Mr.Expert
+1 please add the relevant code to your post though, in case the link ever goes down
BlueRaja - Danny Pflughoeft