For example I have this array:
int a[] = new int[]{3,4,6,2,1};
I need list of all permutations such that 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 algorithm 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.
Do like this...
Visual representation of the 3-item recursive solution: http://www.docdroid.net/ea0s/generatepermutations.pdf.html
Breakdown:
Here is an implementation of the Permutation in Java:
Permutation - Java
You should have a check on it!
Edit: code pasted below to protect against link-death:
If you're using C++, you can use
std::next_permutation
from the<algorithm>
header file: