-4

I have an assignment for school where I get a file with 25 names and I create every possible combination of 2 person groups.

When it has looped through the 25 names it has to do it again and make a new unique list. This has to continue until there are no possible combinations left.
None of the lists are allowed to be the same and 1 person is only allowed to get paired with someone once. So person one cannot be paired with person 2 in 2 different lists.

I have been searching for a while now what algorithm I can use for this.

Any suggestions?

  • This operator is called [Cartesian Product](https://stackoverflow.com/questions/714108/cartesian-product-of-arbitrary-sets-in-java) – GalAbra Jan 21 '18 at 21:07
  • The [selection sort](https://en.wikipedia.org/wiki/Selection_sort) algorithm uses the exact looping structure you need. – Jim Mischel Jan 22 '18 at 18:52

1 Answers1

0

Two nested loops indeed, but not so straightforward: The outer loop goes over all the names and goes on until the penultimate name (one before the last), the inner loop starts with the next name and pairs each name with the outer loop name.

For a short example, suppose there are just 4 names: A, B, C and D. The first iteration of the outer loop gives AB, AC, AD, the second iteration BC, BD and the last iteration CD.

It is not the Cartesian product, which is a set of ordered pairs, the OP specifically said in a rather roundabout way that the order does not matter.

Jonathan Rosenne
  • 2,159
  • 17
  • 27