All possible combinations algorithm

Andrew McNabb amcnabb at
Tue Jun 14 23:25:52 MDT 2005

On Tue, Jun 14, 2005 at 11:03:29PM -0600, 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'
> I need an algorithm that allows me to combine each of these array
> elements into a string... all combinations:
> "hey you guys"
> "hey guys you"
> "you hey guys"
> "you guys hey"
> "guys hey you"
> "guys you hey"
> I'm sure I could pound this out, but I'm sure some of you have done this
> in the past or know a specific algorithm that would do the trick.

I would do this recursively.  My PHP stinks, but something like this:

function hello($array)
	foreach ($array as $i => $value) {
		unset ($array[$i]);
		print $element . hello($array);
		$array[$i] = $value;

Andrew McNabb
PGP Fingerprint: 8A17 B57C 6879 1863 DE55  8012 AB4D 6098 8826 6868
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : 

More information about the PLUG mailing list