Basically, each step will be sorted by the record, inserted into the appropriate position of the recorded record in front of its key code, and can be tail from the head.
Direct insertion
Template
Void Insertsort (T A [], Int N, Int & KCN, INT & RMN)
{
KCN = 0; RMN = 0;
For (INT I = 1; I { T Temp = a [i]; RMN ; For (int J = i; j> 0 && kcn && temp
A [J] = TEMP; RMN ; } } This is the case after a streamlined: Template { For (INT I = 1; I { T temp = a [i]; For (int J = i; j> 0 && temp
a [j] = TEMP; } } Test Results: Sort ascending n = 10000 timeespared: 0ms KCN = 9999 kcn / n = 0.9999 kcn / n ^ 2 = 9.999e-005 kcn / nlogn = 0.07525 RMN = 19998 RMN / N = 1.9998 RMN / N ^ 2 = 0.0001998 RMN / NLOGN = 0.1505 Sort randomness n = 10000 timeespared: 330ms KCN = 24293730 kcn / n = 2429.37 kcn / n ^ 2 = 0.242937 KCN / NLOGN = 182.829 RMN = 24303739 RMN / N = 2430.37 RMN / N ^ 2 = 0.243037 RMN / NLOGN = 182.904 Sort descending n = 10000 timeespared: 711ms KCN = 49995000 kcn / n = 4999.5 kcn / n ^ 2 = 0.49995 kcn / nlogn = 376.25 RMN = 50014998 RMN / N = 5001.5 RMN / N ^ 2 = 0.50015 RMN / NLOGN = 376.4 It can be seen that the average performance is approximately N2 / 4, and there is no deception in the book (nonsense, how many people do more tests can be drawn). Folding sequencing This sorting method can be obtained by sequential searching strategies in the order of order. Obviously, only KCN can only reduce RMN, and the performance improvement can be increased. Template Void BinaryInsertsort (T A [], Int N, Int & KCN, INT & RMN) { KCN = 0; RMN = 0;