Basically, I have an array containing 25 different people, I need to select 5 of these people and have every single combination possible, without using duplicates of the same person.
The only logical way I can think of doing it is by having 5 for loops and checking if person has already been used, although this seems like there’s probably a better method involving recursion.
If anyone can help I’d be very appreciated.
Here’s an example of my class;
public class Calculator {
final Person[] people = new Person[25]; //Pretend we have filled in this info already
public List<Group> generateList()
{
final List<Group> possible = new ArrayList<>();
for (int a = 0; a < 25; a++)
{
for (int b = 0; b < 25; b++)
{
for (int c = 0; c < 25; c++)
{
for (int d = 0; d < 25; d++)
{
for (int e = 0; e < 25; e++)
{
final Group next = new Group();
next.set = new Person[] {
people[a],
people[b],
people[c],
people[d],
people[e]
};
possible.add(next);
}
}
}
}
}
return possible;
}
class Group {
Person[] set = new Person[5];
}
class Person {
String name;
int age;
}
}
However I’m not sure the best way to do this and if that would even get every combination. I also know there’s no duplicate checking here, which I’d do by checking;
if(b == a) continue;
Etc.
I would appreciate any help.
There are many options.
(1)
you can improve your algoritghm by using
etc – this eliminates duplicate checking, taking advantage of the factorial nature of the problem
(2)
you can alternatively implement these as a single for loop over the whole N^R items (if you chose R items from N), and discard permutations that are not in full ascending order. This is good if you don’t know R beforehand. Imagine you are counting in base N
In other words, you count sequentially on these R indexes.
(3)
The final option, which is longer to code but more efficient for large R and N, is to use a set of indices:
then if you go above
Non any index, increasej, incrementi[j], and set all thei[k<j]to[0 1 2 ... j-1], and start counting again!this cycles most efficiently through all combinations.