The essence of the hill sort is the grouping insert sort, which is also known as narrowing the incremental sort, because of the DL. The shell was introduced in 1959 and named
The basic idea of this method is that the whole sequence of elements is divided into several sub-sequences (consisting of elements separated by an "increment"), then the direct insertion sort is done separately, then the increment is reduced and then sorted, and then the whole element is sorted by a direct insertion when the elements in the entire sequence are basically ordered (the increment is small enough). Because the direct insert sort is very efficient when the elements are basically ordered (close to the best case), the hill sort is more efficient in time than the first two methods.
1 voidHellsort (intUnsort[],Const intcount)2 {3 for(intGap = count/2; Gap >0; Gap/=2)4 {5 for(inti =0; I < gap;i++)6 {7 for(intj = I+gap; J < Count; J + =Gap)8 {9 inttemp =Unsort[j];Ten intK = J-Gap; One while(k >0&&unsort[k]>temp) A { -Unsort[k + gap] =Unsort[k]; -K-=Gap; the } -UNSORT[K+GAP] =temp; - } - } + - } +}
Test
1 intMain ()2 {3 intUnsort[] = {2,5,7,4,6,9};4Hellsort (Unsort,6);5 for(inti =0; I <6; i++)6 {7printf"%d\r", Unsort[i]);8 }9 return 0;Ten}
Note: For how to calculate the step size, you still need to consult the data.
Sort algorithm--Hill sort