Select sort: in n-I + 1 (I =,..., n-1) records, select the records with the smallest keyword as the I record in the sequence.
1. Simple selection and sorting
One-stop sorting operation:
Through the comparison between n-I keywords, the records with the minimum keyword are selected from n-I + 1 records, and the records with the I (1 <= I <= N) records are exchanged.
The algorithm for simple selection and sorting of records in L [1... n] is: Enable I to start from 1 to n-1 and perform the n-1 selection operation. In the simple sorting process, the number of operations to move records is relatively small. However, no matter what the initial sorting of records is, the number of comparisons between the keywords is the same. Therefore, the total time complexity is O (n ^ 2 ).
Sample Code (C language description ):
/*************************************** * ***************************** Author: li Bing Date: Email: [email protected] @ array: the pointer to the records @ length: the length of the records *********************************** ********************************** // exchange data void swap (int * num1, int * num2) {int TMP = * num1; * num1 = * num2; * num2 = TMP;} void selectsort (INT array [], int length) {If (array = NULL | L Ength <= 0) return; int I, j, index; for (I = 0; I <length; I ++) {Index = I; for (j = I + 1; j <length; j ++) if (array [J] <array [Index]) Index = J; if (I! = Index) Swap (& array [I], & array [Index]) ;}}
Ii. Tree-based sorting (tournament sorting)
Tree-based sorting is also called tournament sorting. First, the key words of N records are compared in pairs, and then the two smaller ones in n/2 are compared in pairs. This is repeated until the record with the minimum keyword is selected. This process can be represented by a Complete Binary Tree with N leaf nodes.
Code example (C language description ):
/** Filter the tree: The smallest value is selected from the leaf node and the smallest value is selected from each other. */static void adjusttree (int * tree, int length, int high) {/* adjust to the root node and end the adjustment */If (length = 1) {return;} else {int nextlength = (Length + 1)/2; int adjustpos = high-Length + 1-nextlength;/* select two leaves, and select a smaller one as the parent of the two leaves */For (INT I = high-Length + 1; I <= high; I + = 2) {/** only one leaf may exist in the last selection, that is, the odd number of leaves * should still use the remaining leaf tree [I] as their parent * So I
Because the depth of the Complete Binary Tree Containing N leaf nodes is "(log2 (N) + 1", except for the minimum keyword, each time you select a small keyword, you only need to perform a "(log2 (N) comparison, so its time complexity is O (nlogn ). "Indicates the rounded up.
References:
1. Edited by Yan Weimin Wu Weidong, data structure (C language description)
2. http://blog.csdn.net/to_be_it_1/article/details/37866391
3. http://blog.csdn.net/zhccl/article/details/7960983
4. http://blog.csdn.net/morewindows/article/details/6671824
Sort algorithms for algorithm learning: Select sorting