Hill sort
The Hill sort (Shell sort) is a sort of insertion. Also known as narrowing incremental sorting, is a more efficient and improved version of the direct insertion sorting algorithm. Hill Sort is a non-stable sorting algorithm. The method is due to DL. The shell was named after it was introduced in 1959.
The Hill sort (Shell sort) is the entire backlog of record sequences
(R1,r2,r3,......, Rn)
By Delta D is divided into D sub-sequences, where the I (1≤I≤D) sub-sequence is listed as
(ri,ri+d,ri+2d,......, RI+KD)
The sequence of each subsequence is directly inserted, and the process is repeated by decreasing the increment D; Until d is reduced to 1, a direct insert sort is performed for the entire sequence. Sequence of values for increment D
is called an incremental sequence . based on the descending character of the incremental sequence, the hill sort is also known as " narrowing the incremental sort ".
The difference between a hill sort and a direct insert sort is that the direct insert sort compares adjacent records each time, and the record moves at most one position, while the hill sort is separated from each other for a long distance
(i.e. incremental) records are compared so that records can be moved across multiple records to achieve macro adjustments. When the increment decreases by 1 o'clock, the sequence is basically orderly, and the last trip of the hill sort
is the direct insertion sort that approximates the best situation. The "macro" adjustment of the previous trip can be regarded as the preprocessing of the last trip, for example, it is more efficient to do a direct insert sort.
Assuming that the record to be sorted is 10, its corresponding key sequence is listed as
($ 55 04)
There are two 49, followed by a 49 underline to show the difference. If the incremental order is listed as
(5, 3, 1)
The increment d1 of the first trip is 5, and the 10 backlog records are divided into 5 sub-sequences, respectively, the direct insertion is sorted, the result is
(55 04 49 38 65 97 76).
PS: Because the order of the horizontal line to the first 49 front, so the hill sort is an unstable sorting method.
The second trip of the increment D2 is 3, the result of the previous trip is divided into 3 sub-sequences, respectively, the direct insertion sort, the result is
(38 27 49 55 65 97 76).
(38 27 49 55 65 97 76).
The third trip's increment D3 is 1, the entire sequence is directly inserted, and the result is
(49 55 65 76 97).
Code (c language):
1#include <stdio.h>2#include <math.h>3 4 #defineMaxnum 105 6 intMain ()7 {8 voidShellsort (intArray[],intNintT);//T is a sort of number of times9 intarray[maxnum],i;Tenprintf"input numberd: \ n");//input One for(i =0; I <Ten; i++)//Array Ascanf"%d",&array[i]); -printf"\ n"); -Shellsort (Array,maxnum,int(Log (maxnum+1)/log (2)));//the sort pass number should be the integer part of log2 (n+1) theprintf"The sorted numbers: \ n"); - for(i =0; I <Ten; i++)//Output Array -printf"%d", Array[i]); -printf"\ n"); + } - + //Insert sort based on current increment A voidShellinsert (intArray[],intNintDK) at { - Static inti,j,temp; - for(i=dk;i<n;i++)//each group is inserted into an ordered area, respectively. - { -temp=Array[i]; - for(J=I-DK; (J>=I%DK) &&ARRAY[J]>TEMP;J-=DK)//compare with record back move at the same time inarray[j+dk]=Array[j]; - if(j!=i-DK) toArray[j+dk]=temp;//Insert + } - } the * //Calculate Hibbard increments $ intDkhibbard (intTintk)Panax Notoginseng { - return int(Pow (2, t-k+1)-1); the } + A //Hill Sort the voidShellsort (intArray[],intNintt) + { - voidShellinsert (intArray[],intNintDK); $ inti; $ for(i=1; i<=t;i++) - Shellinsert (Array,n,dkhibbard (t,i)); - } the - //This is easy to understand and should be used as a function of the above three functions.
Input/output:
The time complexity of hill sorting is the function of the increment sequence, which is difficult to analyze accurately. It is pointed out that when the increment sequence is d[k] = 2t-k+1-1, the time complexity of the hill sort is O (n1.5), where T is the number of sequential trips, 1≤K≤T≤[LOG2 (n+1)].
Hill sort (C language)-parsing