The hill sort is an upgraded version of the Insert Sort, which works by comparing elements that are spaced apart; the distance used for each comparison decreases as the algorithm progresses until the last order of the adjacent elements is compared. For this reason, the hill sort is also known as narrowing the incremental sort
Hill sort uses a sequence h1,h2,h3,...., HK, called an incremental sequence. As long as the h1=1, any increment sequence is feasible. After a trip of the incremental HK, for each i we have a[i]<=a[i+hk]; all elements separated by HK are sorted. At this point the file is hk-sorted.
For example:
(in 5-sort, that is, the 0,5,10 element is a set of sub-sequences: 81,35,41 is the insertion sort, which is: 35,41,81. The same can be achieved by other 5-sorted sequences and other hk-sort)
That's a trip. The function of hk-sort is to perform one-time insertion sort on HK independent sub-arrays;
Algorithm implementation:
#include <stdio.h> #define MAX 13void shellsort (int *data,int N); int main () { int data[max]={ 81,94,11,96,12,35,17,95,28,58,41,75,15}; int i=0; printf ("Before sort:\n"); for (i=0;i<max;i++) { printf ("%d\t", Data[i]); } Shellsort (Data,max); printf ("\nafter sort:\n"); for (i=0;i<max;i++) { printf ("%d\t", Data[i]); } return 0;} void Shellsort (int *data,int N) { int incre=0;//increment int i=0,j=0; int tmp; for (incre=n/2;incre>0;incre/=2) {for (i=incre;i<n;i++) { tmp=data[i]; for (J=i;j>=incre;j-=incre)//each time it is compared to the number of previous j-incre, the iteration goes down each iteration is an embodiment of the insertion sort { if (Tmp<data[j-incre]) Data[j]=data[j-incre]; Else break;//indicates sorted } data[j]=tmp; }}}
Sort algorithm--Hill sort