fork download
  1. <?php
  2. function pr($array_to_output)
  3. {
  4. print '<pre>';
  5. print_r($array_to_output);
  6. print '</pre>';
  7. }
  8. // Function generating order participants will be placed in array
  9. function getBracket($L) {
  10. // List will hold insert sequence
  11. $list = array();
  12. // Bracket will hold final order of participants
  13. $bracket = array();
  14. // The algorithm to generate the insert sequence
  15. for ($n = 1; $n <= $L; $n += 1) {
  16. // If 'perfect' number, just put it (Perfect no.s: 2, 4, 8, 16, 32, etc)
  17. if (is_int(log($n)/log(2)) || $n == 1) {
  18. $list[] = $n;
  19. // If odd number, stuff...
  20. } elseif ($n % 2 == 1) {
  21. $list[] = $list[($n-1)/2];
  22. // Else even number, stuff...
  23. } else {
  24. $list[] = $list[$n/2-1]+$n/2;
  25. }
  26. }
  27.  
  28. // Insert participant order as per insert sequence
  29. for ($i = 1; $i <= sizeof($list); $i += 1) {
  30. $id = $list[$i-1]-1;
  31. array_splice($bracket, $id, 0, $i);
  32. }
  33. return $bracket;
  34. }
  35.  
  36. // Find number of participants over 'perfect' number if any
  37. function cleanList($L) {
  38. for ($d = 1; $L > $d; $d += 1) {
  39. $diff = $L-pow(2,$d);
  40. if($diff == 0) {break;}
  41. if($diff < 0) {
  42. $diff = pow(2,$d)-$L;
  43. break;
  44. }
  45. }
  46. return $diff;
  47. }
  48.  
  49. $participants = array(
  50. array("John", 0),
  51. array("Gagan", 1),
  52. array("Mike Tyson", 1),
  53. array("Gair", 0),
  54. array("Gale", 0),
  55. array("Roy Johnes", 1),
  56. array("Galip", 0),
  57. array("Gallagher", 0),
  58. array("Garett", 0),
  59. array("Nikolai Valuev", 1),
  60. array("Garner", 0),
  61. );
  62.  
  63. // Extract strength of participant
  64. foreach ($participants as $array) {
  65. $finorder[] = $array[1];
  66. }
  67. // Sort by strength, strongest first
  68. array_multisort($finorder,SORT_DESC,$participants);
  69.  
  70. $order = array();
  71.  
  72. // Remove participants above 'perfect' number
  73. $add = cleanList(sizeof($participants));
  74. for ($r = 1; $r <= $add; $r += 1) {
  75. $participants[] = null;
  76. }
  77.  
  78. // Get corresponding bracket
  79. $res = getBracket(sizeof($participants));
  80. // Align bracket results with participant list
  81. foreach ($res as $n) {
  82. $order[] = $n;
  83. }
  84. array_multisort($order, $participants);
  85. $participants = array_combine($res, $participants);
  86.  
  87. echo "The final arrangement of participants\n";
  88. pr($participants);
  89. ?>
Success #stdin #stdout 0.01s 20568KB
stdin
Standard input is empty
stdout
The final arrangement of participants
<pre>Array
(
    [1] => Array
        (
            [0] => Gagan
            [1] => 1
        )

    [9] => Array
        (
            [0] => Garett
            [1] => 0
        )

    [5] => Array
        (
            [0] => Gair
            [1] => 0
        )

    [13] => 
    [3] => Array
        (
            [0] => Nikolai Valuev
            [1] => 1
        )

    [11] => Array
        (
            [0] => John
            [1] => 0
        )

    [7] => Array
        (
            [0] => Galip
            [1] => 0
        )

    [15] => 
    [2] => Array
        (
            [0] => Mike Tyson
            [1] => 1
        )

    [10] => Array
        (
            [0] => Garner
            [1] => 0
        )

    [6] => Array
        (
            [0] => Gale
            [1] => 0
        )

    [14] => 
    [4] => Array
        (
            [0] => Roy Johnes
            [1] => 1
        )

    [12] => 
    [8] => Array
        (
            [0] => Gallagher
            [1] => 0
        )

    [16] => 
)
</pre>