Java algorithm to print all the combination of the integer array

Hi,

I have an integer array and i want to print all the combinations of this integer array. can you please suggest me the algorithm?

e.g int n[] ={1,2,3}

and its combinations are 123,132,231,213,312,321

problem is that i want the general algorithm which is independent of the array size.

Re: Java algorithm to print all the combination of the integer array

Re: Java algorithm to print all the combination of the integer array

Hi ,

I try and find the algorithm but its acutal combinations are less than expectation.

e.g if i have 6 number then my code gives 126 combinations .

i try other algorithm but it repeats the combinations

so please suggest the link for algorithm for this ........

Re: Java algorithm to print all the combination of the integer array

Would this do it:

Use all digits one at a time in first position

use remaining digits one at a time in next position

etc until no digits left

Re: Java algorithm to print all the combination of the integer array

Re: Java algorithm to print all the combination of the integer array

I think you are asking for:

number of permutations of n distinct objects is n×(n − 1)×(n − 2)×⋯×2×1, which is commonly denoted as "n factorial" and written "n!".

Re: Java algorithm to print all the combination of the integer array

Hi,

Yes, is there any algorithm for it..

Re: Java algorithm to print all the combination of the integer array

What did google say? It usually has algorithms for common problems like this one.

Re: Java algorithm to print all the combination of the integer array

hi,

i found some algorithm but they miss some combinations...

is there any algorithm that print all the combinations and exactly once?

Re: Java algorithm to print all the combination of the integer array

Sounds like you'll have to write the algorithm yourself. Take some time and think about it. If you run into road blocks come ask for help. I could just write the algorithm and give it to you, but that wont help you learn, you need to give it a shot.

Re: Java algorithm to print all the combination of the integer array

Are you searching for combinations or permutations?

This looks like permutations: 123,132,231,213,312,321

What code have you tried?

Re: Java algorithm to print all the combination of the integer array

It seems as if it is a matter of the length of the array. Ignoring the possibility of two of the same number in the array, this should work:

Code :

public int combinations(int length){
if(length == 1){
return 1;
}
else{
return length*combinations(length-1);
}
}

The parameter for this method can be the length of the array. For example, n.length