Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Combinations

Status
Not open for further replies.

nagasekhar

Programmer
Jan 1, 2003
9
IN
I want an algorithm which would give me all the combinations possible with the given numbers. (ie)
for ex: if i give 1,2,3 , it should print all the possible combinations that can be made with those three digits (i.e) 123, 132, 213,231,312,321.
for 1,2,3,4.. it is 4123, 4132, 4213,4231,4312,4321, 1234,1243,1324,1423,1432,1342,2134,2143,2431,2431,2341,2314,
3124,3142,3214,3241,3421,3412.
(i.e) n! number of combinations for the given n digits...
Its urgent . Thank you
 
Would recursive function be enough for you to solve it?
Me thinks people come here to ask to solve their hw for them :] Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
well the straight forward approach would be to create your array of 1-n and then swap elements 0&1, swap em back, swap 1&2, swap em back etc... then shift all the #s to the right (circular) and repeat. example with n = 3

123
213
321 - now shift to make 231 (i.e. element 0 == n)
231
321
132

good luck.

Matt

 
Thank you Zyrenthian,
The approach is good. I will try to implement it and let you know. Thank you for the help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top