Hill sort is also called grouping insert sort, narrow incremental sort.
It works by comparing the elements that are spaced apart; the distance used for each comparison decreases as the algorithm progresses until the last comparison of adjacent elements is compared.
Simply put, the entire sequence is divided into several sub-sequences (consisting of elements that are separated by an increment), and the insertion sort is performed separately. Then, in order to reduce the increment and then to sort, the whole sequence of elements in the basic order, and then the entire element on the line once inserted sort (because this time the whole sequence is basically ordered, with the insertion sort efficiency is higher, sub-sequence is also the same reason).
public static void Shellsort (int[] a) {if (a==null) {return;} int n = a.length;//determines the increment sequence: N/2,2/4,..., 1for (int gap=n/2; gap>0; gap/=2) {for (int i=0; i<gap; i++) {//Insert sort for each sub-sequence for ( int j=i+gap; j<n; J+=GAP) {int temp = a[j];////Note the condition, k>=0 and A[k]>temp, if written as k>0 then the first element is not in the sort for (int k=j-gap; k>=0&&a [K]>temp;k-=gap] {A[k+gap] = a[k];a[k] = temp;}}}}
The time complexity of hill sequencing is better than O (n^2), as it is exchanged for some distant elements in a round order.
Although the ordering of each subsequence is an insertion sort, and the insertion sort is secure, the same size elements may move in the respective insertion sort during the insertion ordering of different sub-sequences, causing the overall stability to be broken, so the hill sort is also unstable.
java-Hill Sort