0

My tournament has two stages, groups + single elimination. When the groups stage ends, the x top of each group go to single elimination stage (brackets format). In the brackets stage there are two basics rules:

  • a winner of a group should not be matched against a winner of another group (only against 2nd or 3rd place)
  • no two teams that played in the same group should be matched against each other.

For example, if we have two groups like these:

Group A -> 1ºA, 2ºA, 3ºA, 4ºA

Group B -> 1ºB, 2ºB, 3ºB, 4ºB

And all teams of each group proceed, knockouts should be exactly:

1ºA vs 4ºB
2ºB vs 3ºA
1ºB vs 4ºA
2ºA vs 3ºB

I'm looking for an algorithm to help me create the knockouts in that order.

For clarify, another example:

if we have four groups like these:

Group A -> 1ºA, 2ºA, 3ºA, 4ºA   
Group B -> 1ºB, 2ºB, 3ºB, 4ºB
Group C -> 1ºC, 2ºC, 3ºC, 4ºC
Group D -> 1ºD, 2ºD, 3ºD, 4ºD

The knockouts should be:

1ºA vs 4ºD
2ºB vs 3ºC
1ºC vs 4ºB
2ºD vs 3ºA
1ºB vs 4ºC
2ºA vs 3ºD
1ºD vs 4ºA
2ºC vs 3ºB

Thanks.

aprencai
  • 396
  • 2
  • 10

1 Answers1

-1

in PHP this would be simple .. You'd just get the length of the second array, and count backwards the keys. Be aware that in almost any language, the logic will be identical with a slight syntax difference depending on language. JS, Python, PERL, Java etc etc should all look roughly the same.

<?php

$group_a = array();
$group_a[0] = '1';
$group_a[1] = '2';
$group_a[2] = '3';
$group_a[3] = '4';

$group_b = array();
$group_b[0] = '1';
$group_b[1] = '2';
$group_b[2] = '3';
$group_b[3] = '4';

// If team counts don't match, stop program
if ( count($group_a) != count($group_b) ){
    die("Group team counts do not match");
}

// Set key to group b array count
$group_b_key = count($group_b);

foreach($group_a as $item){
    // count backward from 4 ( 4-1 = 3 )
    $group_b_key --;
    echo "$item-A  vs  " . $group_b[$group_b_key] . "-B\n";
}

This should output:

1-A  vs  4-B
2-A  vs  3-B
3-A  vs  2-B
4-A  vs  1-B
Zak
  • 6,976
  • 2
  • 26
  • 48
  • Hi Zak, thanks for reply. The matches are correct, but i need generate mathes in correct order in the bracket. That is the question. 1ºA vs 4ºB, 2ºB vs 3ºA, 1ºB vs 4ºA, 2ºA vs 3ºB – aprencai Nov 19 '21 at 12:18
  • i've updated the post with another example. – aprencai Nov 19 '21 at 12:28