9.6.3 Hill Sorting algorithm
Well, in order to really figure out the algorithm for hill-sorting, we're doing the old fashioned way-to simulate how the computer performs the algorithm and how the algorithm is sorted.
Hill sorting algorithm code is as follows.
void Shellsort (SqList *l)
{
int i,j;
int increment=l->length;
Do
{
increment=increment/3+1; /* Increment sequence *
/for (i=increment+1;i<=l->length;i++)
{
if (l->r[i]<l->r[i-increment]) / * Need to insert l->r[i] into an ordered increment child table/
{
l->r[0]=l->r[i]; /* Temporary Presence l->r[0] * * for
(j=i-increment;j>0 && l->r[0]<l->r[j];j-=increment)
l->r[j +increment]=l->r[j]; /* Record back, find insertion position * *
l->r[j+increment]=l->r[0]; * Insert/}} while
(increment>1);
1 The program starts to run, at this time we pass in the value of the SqList parameter is length=9,r[10]={0,9,1,5,8,3,7,4,6,2}. This is the sequence we need to wait for sorting, as shown in Figure 9-6-4.
2 The 4th row, the variable increment is that "increment", our initial value makes it equal to the number of records to be sorted.
3 the 5th to 19th line is a Do loop, which refers to the termination condition that increment is not more than 1 o'clock. In fact, the increment is 1 o'clock to stop the cycle.
4 The 7th line, this sentence is very critical, but also difficult to understand the place, we have to talk about it, first put aside. Here the execution is done, increment=9/3+1=4.
5 Line 8th to 17th is a for loop, I start from 4+1=5 to 9 end.
6 line 10th, Judge L.r[i] and l.r[i-increment] size, l.r[5]=3<l.r[i-increment]=l.r[1]=9, meet the conditions, line 12th, will l.r[5]=3 temporary deposit l.r[0]. The 13th to 14th line of the loop is just to assign the value of the l.r[1]=9 to L.r[5], because the increment of the loop is j-=increment, in fact it loops once, at this time j=-3. Line 15th, and then assign the l.r[0]=3 to l.r[j+increment]=l.r[-3+4]=l.r[1]=3. As shown in Figure 9-6-5, in fact, this piece of code does one thing, which is to swap the position of the 5th Digit 3 and 1th 9.
7 The loop continues, i=6,l.r[6]=7>l.r[i-increment]=l.r[2]=1, and therefore does not exchange both data. As shown in Figure 9-6-6.
8 cycle continue, i=7,l.r[7]=4<l.r[i-increment]=l.r[3]=5, exchange both data. As shown in Figure 9-6-7.
9 cycle continue, i=8,l.r[8]=6<l.r[i-increment]=l.r[4]=8, exchange both data. As shown in Figure 9-6-8.
10 cycle continue, i=9,l.r[9]=2<l.r[i-increment]=l.r[5]=9, exchange both data. Note that line 13th to 14th is a loop, at which point the size of l.r[5] and l.r[1 will continue to be compared because of the 2<3, so the data for l.r[5 and L.r[1 is also exchanged, as shown in Figure 9-6-9.
After the end of the first round, the order of the arrays is shown in Figure 9-6-10. Careful students will find that our numbers 1, 2 and other small numbers have been in the top two, and 8, 9 and other large numbers have been in the back two, that is, through such a sort, we have let the whole sequence of basic order. This is actually the essence of hill sort, it will be a small record of the keyword, not step by step forward, but move forward, so that every time after the completion of a round of circulation, the entire sequence of a solid step towards the orderly.
11 We continue, after a round of Do loop, at this time due to increment=4>1 so we need to continue do loop. Line 7th gets increment=4/3+1=2. Line 8th to 17th for loop, I start from 2+1=3 to 9 end. When i=3, 4 o'clock, do not exchange, when i=5, you need to exchange data, such as Figure 9-6-11
12 Thereafter, I=6, 7, 8, 9 are not exchanged. As shown in Figure 9-6-12
13 Complete one round do loop again, increment=2>1, do Loop again, 7th line get increment=2/3+1=1. This is the last round of Do loop at this point. Although the 8th to 17th line for loop, I start from 1+1=2 to 9 end, but because the current sequence is basically orderly, exchangeable data is greatly reduced, the efficiency is actually very high. As shown in Figure 9-6-13, the arrows in the diagram are connected to the keyword that needs to be exchanged.
Finalize the sorting process, as shown in Figure 9-6-14.
Analysis on the order complexity of 9.6.4 Hill
Through the analysis of this code, I believe that we have some understanding, hill sort of the key is not casually grouped after their respective sorting, but will be separated by a "increment" of the record to form a subsequence, to achieve a jump-type movement, so that the efficiency of the ranking improved.
The choice of "increment" here is very critical. We are in the code line 7th, is using increment=increment/3+1 to select the increment, but exactly what should choose the increment is the best, is still a mathematical problem, so far no one has found the best incremental sequence. However, a large number of studies have shown that when the increment sequence is Dlta[k]=2t-k+1-1 (0≤k≤t≤⌊log2 (n+1) ⌋), it can achieve good efficiency, the time complexity of O (N3/2), better than the direct ordering O (n2). It is important to note that the last increment value of the increment sequence must be equal to 1. In addition, because the record is a jump-type movement, hill sorting is not a stable sort algorithm.
In any case, the discovery of the hill sorting algorithm has made us finally break through the slow sequencing era (beyond the time complexity of O (N2)), and then the corresponding more efficient sorting algorithms have emerged.
Source: http://www.cnblogs.com/cj723/archive/2011/04/20/2021648.html