In the league system where games are played today, if n people sign up for the League, the server records the number of registrants and sorts the results after the battles, determine the Top 16 or top 8.

I found a ranking algorithm for the championship online, with little content. Summary:

1: Build a tree: here we talk about building a Complete Binary Tree. When the number of times of the sorted array is less than 2, supplement it. Until a Complete Binary Tree is created

2: When the competitor of a node shows that the node is "no longer running", the node is automatically promoted to the parent node, which is not counted in the number of sort code comparisons. The first place after each comparison is marked as not participating.

3: The final result is returned to the original array.

4: heap sorting. There are many materials, not to mention.

/*** League system group ** @ author administrator */public class competition {/*** change player to player ID storage */private player [] playerarray; public competition () {int playernum = World. getins (). getcompetition (). size (); player [] playerarray = (player []) World. getins (). getcompetition (). toarray (); tournamentsort (playerarray, playernum); // sort the competition results in the array order, put in playerarray}/*** create a tree * @ Param A * @ Param N */public static void tourn Amentsort (player [] player, int N) {tournamentplayer [] tree; int bottomrowsize = nearestpoweroftwo (n); // calculate the number of minimum power values that meet the power of 2> = N: int treesize = 2 * bottomrowsize-1; int loadindex = bottomrowsize-1; // start position of the external node: Start from the root node and run down tree = new tournamentplayer [treesize]; for (INT I = 0; I <treesize; I ++) {tree [I] = new tournamentplayer () ;}int J = 0; // obtain the Data Pointer for (INT I = loadindex; I <treesize; I ++) in the array player {// number of copies Group data to the external node of the tree tree [I]. setindex (I); // subscript if (j <n) {tree [I]. setactive (1); tree [I]. setdata (player [J ++]);} // copy data else {tree [I]. setactive (0); // The outer node after the node is empty} // system. out. println (tree [I]. getindex () + "" + tree [I]. getdata ();} int I = loadindex; // perform an initial comparison to find the smallest item while (I! = 0) {J = I; while (j <2 * I) {// process each competitor player playerright = tree [J]. getdata (); player playerleft = tree [J + 1]. getdata (); // @ todo computing battle, returns the left-side win Boolean isleftwin = true; If (tree [J + 1]. getactive () = 0 | isleftwin = true) tree [(j-1)/2] = tree [J]; // The winner sends elsetree [(j-1)/2] = tree [J + 1]; // system. out. println (tree [(j-1)/2]. getindex () + "" + tree [(j //-1)/2]. getdata (); j + = 2; // The next comparison} I = (I-1)/2; // I is returned to the parent until I = 0 // system. out. println (tree [(j-1)/2]. getindex () + "" // + tree [(j-1)/2]. getdata () ;}for (I = 0; I <n-1; I ++) {// process other n-1 elements player [I] = tree [0]. getdata (); // the current minimum element is sent to the array asystem. out. println (player [I]); tree [tree [0]. getindex ()]. setactive (0); // the corresponding external node of the element does not match updatetree (Tree, tree [0]. getindex (); // modify from the preceding position} player [n-1] = tree [0]. getdata (); system. out. println (Player [n-1]); // return tree;}/*** after each winner is compared, update to get the winner of the next comparison * I is the subscript of the current smallest element in the table, that is, the winner. Start from it and adjust it up. * @ Param tree * @ Param I */public static void updatetree (tournamentplayer [] tree, int I) {if (I % 2 = 0) tree [(I-1) /2] = tree [I-1]; // I is an even number, and the opponent is the left node elsetree [(I-1)/2] = tree [I + 1]; // I is an odd number, and the opponent is the right node // After the smallest element is output, its opponent rises to the parent node position I = (I-1)/2; Int J = 0; // I rises to the parent node position while (I! = 0) {if (I % 2 = 0) J = I-1; // determine whether the opponent of I is the left node or the right node elsej = I + 1; if (tree [I]. getactive () = 0 | tree [J]. getactive () = 0) {// The competitor has an empty if (tree [I]. getactive ()! = 0) tree [(I-1)/2] = tree [I]; elsetree [(I-1)/2] = tree [J];} // non-empty players go up to the parent node else // The competitor is not empty. {player playerright = tree [I]. getdata (); player playerleft = tree [J]. getdata (); // @ todo computing battle, returns the Boolean isleftwin = true on the left; If (isleftwin) tree [(I-1)/2] = tree [I]; elsetree [(I-1)/2] = tree [J]; // The winner rises to the parent node I = (I-1)/2; // I rises to the parent node }}/ *** to obtain the nearest N side * @ Param Number * @ return */public static int nearestpoweroftwo of 2 greater than * Number (INT number) {-- number; number | = number> 16; number | = number> 8; number | = number> 4; number | = number> 2; number | = number> 1; ++ number; return number;} public static void main (string [] ARGs) {player [] relust = new competition (). getplayerarray ();} public player [] getplayerarray () {return playerarray;} public void setplayerarray (player [] playerarray) {This. playerarray = playerarray ;}}