I’m trying to make a sorting program to sort tournament matches.
By entering names of contestant i want these to be sorted in a specific way. Criterias are that no person can go to matches after eachother, but everyone has to face eachother.
example:
1 - 2
1 - 3
1 - 4
1 - 5
2 - 3
2 - 4
2 - 5
3 - 4
3 - 5
4 - 5
These are all the matches and they have to be sorted/organized in such manner that no one as close as possible cannot be in matches after each other.
I know for a mathematical fact that it isn’t possible to solve so that everyone doesn’t go two matches after each other. But i want the “sorting” to be as close as possible.
It has to be a bit “smart” in that sense that it tries its best.
The sorting has to be dynamic so if i enter 7 contestants up to 30 contestants the sorting will try its best.
Right now i have the contestants either in the right order say 1-17 in an arraylist, or i have them in all the permutations in an arraylist like: 1, 2, 1, 3, 1, 4, 2, 3, 2, 4, 3, 4.
I have tried with comparing numbers, iterating in different ways but im dead in the water. Im asking for help, or someone that can point me in the right direction so i can turn
this:
1 - 2
1 - 3
1 - 4
1 - 5
2 - 3
2 - 4
2 - 5
3 - 4
3 - 5
4 - 5
to this:
5-4
1-2
3-4
1-5
2-3
1-4
2-5
3-1
5-4
3-5 <--here number 5 has to go 2 matches in a row
I propose a “random” solution to your pairings so that the contestants can’t predict their placement from year to year (assuming the event is annual).
My solution starts by making the combinations of all pairings. The randomizer function swaps two random pairings.
The “count-back-to-backs” function counts the number of pairings in which a contestant goes back to back. This is a metric … or criteria … that you judge the array of pairings by.
The main loop runs a large number of randomizer loops and remembers the configuration with the fewest back-to-back contests. You can vary the number of loops.
You can also add other functions to gather metrics about a particular set of pairings and keep/reject based on those metrics.
This solution is thus: (1) generate random pairings and (2) keep/reject the array of pairings based on one or more criteria and (3) let the computer generate a large number of these random sets for your judgement.
The output is the array of pairings with the number of back-to-backs on the end like
[3 – 5, 2 – 4, 1 – 5, 2 – 3, 1 – 4, 2 – 5, 3 – 4, 1 – 2, 4 – 5, 1 – 3]0