Original link: http://www.orlion.ga/193/
You can see the hill sort by agreeing to an interval (4 in the figure), then inserting the data for 0, 4, 8, three, and then moving right one to the position 1, 5, 9 for insertion sort by this rule until all the sort is involved. The interval is then contracted to 4-1=3, and then the sorting method is continued as above. The process is as follows:
9 1 2 3 0 4 5 7 6 8
SETP 1 after the interval of 4 sorted into: 0 1 2 3 6 4 5 7 9 8
SETP 2 After the interval of 3 sorted into: 0 1 2 3 6 4 5 7 9 8
SETP 3 after the interval of 2 sorted into: 0 1 2 3 5 4 6 7 9 8
SETP 4 after the interval of 1 sorted into: 0 1 2 3 4 5 6 7 9 8
Reduce the interval: for large arrays should be larger at the beginning of the interval, and the reduction of the interval should also be larger, such as for a 1000-size array to be sorted by the interval: 364, 121, 40, 13, 4, 1, here the interval sequence is presented by Knuth, The sequence starts in reverse form 1 and is generated by the recursive expression h=3*h+1, with an initial value of 1. (Other methods can also produce sequences) as shown in the following table
Here's The hill sort Java code
Public static void shellsort (Long[] arr , int elenum) { int i,j; long temp; // first Find out h int h = 1; while (h < elenum / 3) H = 3 * h + 1; // 1, 4 while (h > 0) {for (i = h ; i < elenum ; i = i + &NBSP;H) { // sort pairs of arrays temp = arr[i]; j = i; while (j > h-1 && temp &NBSP;<=&NBSP;ARR[J-H]) { arr[j] = arr[j-h]; j-=h; } arr[j] = temp; }h = (h-1)/3; } system.out.println ( Arrays.tostring (arr));}
Hill sort and Hill sort Java code