Text description
Tree Selection Sort is also known as tournament ranking; For example, in 8 athletes to decide the top 3 to need up to 11 games, rather than 7+6+5=18 game (it is the premise of a win B, B wins C, a must be able to win C)
A 22 comparison of the keywords for n Records is performed first, and then a 22 comparison is made between the smaller (N/2), until a record of the smallest key is selected, and the process can be represented by a complete binary tree with n leaf nodes. The definition of a complete binary tree and the nature of this sorting algorithm are shown in Appendix 1
Algorithm analysis
Since the depth of the complete binary tree with n leaf nodes is [log2n]+1, in the tree selection sort, in addition to the minimum keyword, each sub-keyword is selected for log2n times, so its time complexity is NLOGN. But it needs a secondary space of 2*n-1. Moreover, it makes a redundant comparison with the "maximum" during the selection process. In addition, the algorithm is unstable.
Code implementation
1#include <stdio.h>2#include <stdlib.h>3#include <math.h>4 /*5 * Double log2 (double x); Logarithm with base 26 * Double ceil (double x); Take the whole7 * Double floor (double x); Remove the entire8 * Double fabs (double x); Take absolute value9 */Ten One #defineDEBUG A - #defineEQ (A, B) ((a) = = (b)) - #defineLT (A, B) ((a) < (b)) the #defineLQ (A, B) ((a) <= (b)) - - #defineMAXSIZE 100 - #defineINF 1000000 +typedefintKeyType; -typedefCharInfoType; +typedefstruct{ A KeyType key; at InfoType Otherinfo; - }redtype; - -typedefstruct{ -Redtype r[maxsize+1]; - intlength; in }sqlist; - to voidprintlist (sqlist L) { + inti =0; -printf"Subscript Value:"); the for(i=0; i<=l.length; i++){ *printf"[%d]", i); $ }Panax Notoginsengprintf"\ n Keyword:"); - for(i=0; i<=l.length; i++){ the if(EQ (L.r[i].key, INF)) { +printf"%-3c",'-'); A}Else{ theprintf"%-3d", L.r[i].key); + } - } $printf"\ n Other values:"); $ for(i=0; i<=l.length; i++){ -printf"%-3c", l.r[i].otherinfo); - } theprintf"\ n"); - return ;Wuyi } the - /*Tree selection sorting algorithm*/ Wu voidTreeselectsort (SqList *L) - { About //The secondary tree required for this sort to be implemented $ sqlist Tree; - //size of the secondary tree -Tree.length = l->length-1+ l->length; - Atree.r[0].otherinfo ='0'; + inti =0, low =1; the //The leaf nodes of this tree are filled by the back forward . - for(i=0; i<l->length; i++){ $Tree.r[tree.length-i] = l->r[l->length-i]; the } the //The non-leaf nodes of this tree are filled by the back forward . the for(I= (tree.length-l->length); i>=1; i--){ theTree.r[i] = (LT (tree.r[2*i].key, tree.r[2*i+1].key)? tree.r[2*i]:tree.r[2*i+1]); - } in //Record the minimum node of the current secondary tree the Redtype minred; the //Record the subscript value of the minimum node in the leaf node About intMinindex =0; the while(Low <= l->length) { theminred = tree.r[1]; the #ifdef DEBUG +printf"%d goto tree selection sorted, output current number min%d,%c\n", Low, Minred.key, minred.otherinfo); - printlist (tree); the #endifBayi //constant removal of the minimum junction point thel->r[low++] =minred; theMinindex =tree.length; - //find the lowest value in the secondary tree leaf node subscript value - for(Minindex=tree.length; (Minindex> (tree.length-l->length)); minindex--){ the if(EQ (Tree.r[minindex].key, Minred.key) &&EQ (Tree.r[minindex].otherinfo, Minred.otherinfo)) { the Break; the } the } - //set a maximum value flag, INF denotes infinity theTree.r[minindex].key =INF; the //Resize this secondary tree to minimize the root node key value the for(I= (minindex/2); i>=1; I/=2){94Tree.r[i] = (LT (tree.r[2*i].key, tree.r[2*i+1].key)? tree.r[2*i]:tree.r[2*i+1]); the } the } the #ifdef DEBUG98printf"The original order table after sorting by tree selection: \ n"); AboutPrintlist (*L); - #endif101 }102 103 intMainintargcChar*argv[])104 { the if(ARGC <2){106 return-1;107 }108 sqlist L;109 inti =0; the for(i=1; i<argc; i++){111 if(i>MAXSIZE) the Break;113L.r[i].key =atoi (Argv[i]); theL.r[i].otherinfo ='a'+i-1; the } theL.length = (I-1);117l.r[0].key =0;118l.r[0].otherinfo ='0';119printf"input data: \ n"); - printlist (L);121 //Sorting the Sequence table L as a tree selection122Treeselectsort (&L);123 return 0;124}
Tree Selection Sort
Run
Appendix 1 Complete Binary Tree
definition: set two fork tree depth of H, in addition to the H layer, the other layers (1 ~ h-1) The number of nodes reached the maximum, the H layer all the nodes are concentrated on the leftmost, this is a complete binary tree.
Properties:
1] node I (i>1) of the parent node is I/2.
2] The left child node of node I is 2*i and the right child node is 2*i+1.
3] leaf knot point N0, Degree 2 (with left, right child node) node N2, then N0 = n2+1
Property 3] Proof:
Set N1 to a two-fork tree with a 1-point node. Because the degree of all nodes in the binary tree is not greater than 2. So the node number of the binary tree is n = n0 + n1 +N2--(1).
and root node, the rest of the nodes have a branch into, set B for the total number of branches, then n=b+1. Since these branches are burst out of a junction of 1 or 2, there is b=n1+2*n2, so n = b+1 = n1+2*n2+1– (2).
Known by (1) and (2), n0 = N2 +1
Sort by selection, select sorting, tree selection