The routines behind the test program are sorted to the array, and the use of static linked lists also apply to the sort of the linked list. For simplicity, only the single key code is sorted, and the last result is arranged in an ascending order from beginning to tail. Below is a unified test program:
#include ) SWAP (Randomness [I], randomness [random (n)]); cout << "sort assending n =" << n; testing; cout << "sort randomness n = << n; test (TEST) Randomness); cout << "sort descending n =" << n; testing; return 0;} Need to explain a point, KCN (number of keyword comparisons), RMN (number of recordings) is not algorithm, In order to have an intuitive evaluation of the performance of the algorithm (without those formulas calculated). Sorting more than 10,000 integer should be the most surprising test means. It is recommended not to increase the number of records. First, in the worst case, it is necessary to wait for too long. When it is worse, the number of records may overflow, causing the program to crash. Insert Sort Basic Thought is that a record of to be sorted, 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 This is the case after a streamlined: Template Test Results: Sort ascending n = 10000 Timespared: 0mskcn = 9999 kcn / n = 0.9999 kcn / n ^ 2 = 9.999E-005 kcn / nlogn = 0.07525RMN = 1999 RMN / N = 1.9998 RMN / N ^ 2 = 0.00019998 RMN / NLOGN = 0.1505 sort randomness N = 10000 TimeSpared: 330msKCN = 24293730 KCN / N = 2429.37 KCN / N ^ 2 = 0.242937 KCN / NlogN = 182.829RMN = 24303739 RMN / N = 2430.37 RMN / N ^ 2 = 0.243037 RMN / NlogN = 182.904Sort descending N = 10000 Timespared: 711 mskcn = 49995000 kcn / n ^ 2 = 0.49995 kcn / nlogn = 376.25 RMN = 50014998 RMN / N = 5001.5 RMN / N ^ 2 = 376.4 It can be seen that the average performance Approximate to N2 / 4, there is no deception (nonsense, how many people do how many tests have been drawn). This sorting method can be obtained by folding the search strategy in the direct insertion sort by order search. Obviously, only KCN can only reduce RMN, and the performance improvement can be increased. Template Test Results: Sort ascending N = 10000 TimeSpared: 0msKCN = 123617 KCN / N = 12.3617 KCN / N ^ 2 = 0.00123617 KCN / NlogN = 0.930311RMN = 19998 RMN / N = 1.9998 RMN / N ^ 2 = 0.00019998 RMN / NlogN = 0.1505Sort randomness N = 10000 TimeSpared: 320msKCN = 118987 KCN / N = 11.8987 KCN / N ^ 2 = 0.00118987 KCN / NlogN = 0.895466RMN = 24303739 RMN / N = 2430.37 RMN / N ^ 2 = 0.243037 RMN / NlogN = 182.904Sort descending N = 10000 TimeSpared : 631mskcn = 113631 kcn / n = 11.3631 kcn / n ^ 2 = 0.00113631 kcn / nlogn = 0.855158RMN = 5001.5 RMN / N ^ 2 = 0.50015 RMN / NLOGN = 376.4 can see KCN approximation NLOG2N, there is A certain performance is improved. Table Insert Sort If you use the "pointer" to indicate the order in the record, you can avoid a lot of record movement, of course, or finally rearrange it according to the "pointer". Natural, folding is not used here. Template Test Results: Sort ascending n = 10000 Timespared: 751mskcn = 49995000 kcn / n = 4999.5 kcn / n ^ 2 = 0.49995 KCN / NLOGN = 376.25RMN = 0 RMN / N = 0 RMN / N ^ 2 = 0 RMN / NLOGN = 0sort randomness n = 10000 TimeSpared: 621msKCN = 25721250 KCN / N = 2572.13 KCN / N ^ 2 = 0.257213 KCN / NlogN = 193.572RMN = 29955 RMN / N = 2.9955 RMN / N ^ 2 = 0.00029955 RMN / NlogN = 0.225434Sort descending N = 10000 TimeSpared: 0mskcn = 9999 kcn / n = 0.9999 kcn / n ^ 2 = 9.999E-005 kcn / nlogn = 0.07525RMN = 15000 rmn / n = 1.5 RMN / N ^ 2 = 0.00015 RMN / NLOGN = 0.112886 can be seen, indeed reduced RMN, theoretically RMNMAX = 3 (N-1). However, in the average of the average, performance is not as simple as simple - this is because the test object is an integer. For linked lists, this method does not require the final rearrangement. The algorithm for rearrangement has a detailed description of the "Data Structure (C language version)" in severeity. The average efficiency of the algorithm in Hill is not very good, but we note that the direct insertion sorting is in the case of the critical code, the efficiency is the best, and when the number of key codes is small, N The gap between N2 is not so obvious. Based on the above facts, DLShell proposed narrowing incremental sorting in 1959 (old antiques). Basic idea is: Take an interval (GAP), divide the sequence into several subsequences, and insert each subsequences; then Gradually shrink interval, repeat the above process until the interval is 1. At the beginning, there is very little key in each sub-sequence, and the intersection is high; with the narrowing of the interval, the key code of the subsequence is increasing, but the key code has basically In order, the efficiency of direct insert is still high. Hill sorted time complexity is not well estimated, GAP's selection has not contained, the program of GAP = [GAP / 2] is best written, as for why, I will know.