Hill sorting: downgrading incremental sorting is a sort of insert sorting. According to the previous direct insertion time analysis, the time complexity is O (n ^ 2 ), if the records to be sorted are basically ordered, the time complexity can be increased to O (n)
Basic Idea: first, the entire sequence of records to be sorted is divided into several sub-sequences for direct insertion and sorting. When the whole sequence is basically ordered, all the records are inserted and sorted directly at a time.
Features: 1) subsequences are not simple segments, but are separated by an incremental record to form a subsequence.
2) The increment must be a prime number or a prime number, and the last increment must be 1 to complete a complete sorting.
See the following shell Sorting Algorithm:
Int ShellInsert (MergeType * L, int dk) // sever sort {int nCompKey; int j =-1; for (int I = dk; I <L-> len; I ++) {if (L-> elem [I] <L-> elem [I-dk]) {nCompKey = L-> elem [I]; for (j = I-dk; j> = 0; j-= dk) {if (nCompKey> = L-> elem [j]) {break ;} l-> elem [j + dk] = L-> elem [j];} L-> elem [j + dk] = nCompKey ;}} return 0 ;}Here, the incremental value is 1 instead of 1 when the comparison is directly inserted, but dk. The others are the same, as shown in the following code:
int ShellSort(MergeType* L, int* dlta, int len){if (!L->elem){return -1;}for (int i = 0; i != len; i++){ShellInsert(L, dlta[i]);}return 0;}Time complexity of the algorithm: When the incremental sequence is dlta [k] = 2 ^ (t-k + 1)-1, the time complexity of the hill sort is O (n ^ 1.5 ), in short, time sorting is more complex than direct insertion sorting.
The test procedure is as follows. For other function definitions, see bubble Algorithm Improvement.
# Define LISTLEN 10 MergeType pList; pList. elem = (int *) malloc (sizeof (int) * LISTLEN); pList. len = LISTLEN; pList. size = LISTLEN; ScanfList (& pList); // Hill sorting int nDlta [] = {5, 3, 1}; ShellSort (& pList, nDlta, ARRLEN (nDlta )); printList (& pList); free (pList. elem); free (pT. elem); pList. elem = NULL; pT. elem = NULL;