all possible permutation

Hello all,

I have an array of characters and I want all possible permutations of it. I have seen some threads in forums but i couldn't find the exact one.

For example char ch[]={'a','b','c'};

Output should be

a,b,c,

aa,ab,ac,ba,bb,bc,ca,cb,cc,

aaa,aab,aba,aac,aca,abb,abc,acb,acc,...................................

If this question is posted already please let me know

Thanks in advance

[449 byte] By [ravengaa] at [2007-11-27 3:22:14]
# 1
Those are not permutations.I guess this is a homework assignment? Shouldn't you be doing this yourself? Feel free to post a specific question on the forum, of course.
prometheuzza at 2007-7-12 8:25:00 > top of Java-index,Other Topics,Algorithms...
# 2

has this been posted before? Numerous times and usualy neatly filed under a helpful index topic like "Help needed, Urgent!"

However the example that you gave was NOT a permutation of your original set of characters.

a is not a permutation of abc. it is a subset of abc

bca, bac, abc, acb, cab, cba,

those are the permutations of your set, ie. all elements in some different order.

However your list was also not the list of subsets of a given set because

aa and aab are not subsets of abc

You allow multiple occurances of a element from your original set.

Because of the multiple occurances with no apparent limit, the list you have shown is clearly infinite. Even if we could write the code to generate them all, it would take forever to run.

None the less, the set you have described is fairly easy to code when you realize that what you are doing is essentially counting in some base.

change the letters abc to the digits 012 and look at your list

0,1,2, // one digit numbers in base 3

00,01,02,10,11,12,20,21,22 // two digit numbers in base 3

000,001,010,002,020,011,012,021,022,... // some of the 3 digit numbers in base 3 in an almost numerical progression ....

Does that help?

marlin314a at 2007-7-12 8:25:00 > top of Java-index,Other Topics,Algorithms...