Hill sort is also known as narrowing the incremental sort, this sort method first groups the whole unordered sequence, set the size of each group is the grouping Factor DK. After the completion of the group, the first and the I+DK, I+2DK, i+3dk ... Element is a group. You can then sort the group in some way, using the insert sort.
After each group is ordered, a sequence of "ordered" sequences is obtained. Then reduce the value of the grouping Factor DK. To do this sort of thing again. The last sort is done until the DK is 1 o'clock.
This will sort out the entire sequence.
The following is an example of a code implemented using the interchange Sort method:
Public voidShellsort (int[] ary) { for(varDK = ary. Length/2; DK >=1; DK = DK/2) { //group the elements with a distance of DK to scan all groups for(inti = DK; i < ary. Length; i++) { intj =0; inttemp = Ary[i];//The value that holds the current value, the corresponding position of the last group//Sort the group of elements with a distance of DK, for the first order, each group must have a maximum of 2 elements, which can be understood as a sort of exchange. //compare two values from the last set, forward, corresponding position, and actually insert sort for(j = i-dk; J >=0&& temp < ARY[J]; j = J-DK) {Ary[j+ DK] =Ary[j]; } ary[j+ DK] =temp; } } }
Insert sort--3 Hill Sort implementation