All possible combinations algorithm

Jordan Gunderson jordy at gundy.org
Wed Jun 15 09:33:51 MDT 2005


Dan Wilson wrote:
> Ok folks, I need the help of some good mathematicians/programmers.  I
> know some of you are very good with this type of stuff and I am not.
> 
> Given an array of elements (with an unknown length), I need to find all
> possible combinations of that array.  Say I have an array with the
> values of:
> 
> 'hey', 'you', and 'guys'

Hey Dan,

Thanks for the math question.  If the number of elemnts is E, the number 
of possible arrangements is equal to the E! (read "E factoral").  It's E 
times all of the integers between E and zero.  By careful, because it 
gets really big really fast.

In this example, I started out with one element.  In each following 
sequence I placed the next element in all of the possible positions from 
the preceding element (separated by dashes), starting in the last 
position and moving forward.

Thanks for the fun question.  Jordy
______________________________

A     1 Elements = 1! = 1 = 1

AB    2 Elements = 2! = 2*1 = 2
--
BA

ABC   3 Elements = 3! = 3*2*1 = 6
ACB
CAB
---
BAC
BCA
CAB

ABCD  4 Elements = 4! = 4*3*2*1 = 24
ABDC
ADBC
DABC
----
ACBD
ACDB
ADCB
DACB
----
CABD
CADB
CDAB
DCAB
----
BACD
BADC
BDAC
DBAC
----
BCAD
BCDA
BDCA
DBCA
----
CABD
CADB
CDAB
DCAB
----



More information about the PLUG mailing list