sorting tournament seeds

前端 未结 6 1237
夕颜
夕颜 2021-02-09 17:40

I\'m making a HTML/JS powered single/double elimination bracket web app. I am struggling to figure out how to assign the first round matches from a list of seeded teams/players.

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-09 18:25

    I've come up with a solution, but it's outside the scope of just "sorting arrays".

    The (javascript) code is at http://jsbin.com/ukomo5/2/edit.

    In basic terms, the algorithm assumes that no upsets will occur in the bracket, therefore seeds 1 and 2 should meet in the final round. It iterates through each seed in each round (starting from the pre-calculated grand final, working backwards), calculating the unknown seed in the match in the previous round that the current seed (in the iteration) had won. This can be done because given a seed and round number, you can work out what the other seed should be:

    other seed = number of seeds in round + 1 - the known seed

    To illustrate, in the semifinals:

    Semifinal 1 (where known seed is 1): other seed = 4 + 1 - 1 = 4

    Semifinal 2 (where known seed is 2): other seed = 4 + 1 - 2 = 3

    I just noticed this pattern when looking at a "no upsets" bracket I had drawn.

    In the final iteration (ie round 1) all seeds and their position are known, ready to be assigned to matches. The correct sorted array is below:

    1,16,8,9,4,13,5,12,2,15,7,10,3,14,6,11

    Thanks again to Matt Ball who came up with a correct solution for small brackets (It's difficult to state the problem and desired solution without detailed context, which I didn't do completely in my initial question).

    If anyone has another solution or a more elegant version of my solution let us know!

提交回复
热议问题