Java-hill sorting
Hill sorting is also called grouping insertion sorting and narrowing down incremental sorting.
It works by comparing elements with a certain interval. The distance used for comparison decreases with the algorithm until only the last comparison of adjacent elements is compared.
Simply put, the whole sequence is first divided into several sub-sequences (composed of elements separated by an increment) for insertion sorting. Then, the incremental data is reduced in sequence and then sorted. When the elements in the entire sequence are basically ordered, the data is inserted and sorted once again (because the whole sequence is basically ordered at this time, insert sorting is more efficient. This is also true when sorting sub-sequences ).
Public static void shellSort (int [] a) {if (a = null) {return;} int n =. length; // determine the incremental sequence as: n/4 ,..., 1for (int gap = n/2; gap> 0; gap/= 2) {for (int I = 0; I
= 0 and a [k]> temp. If k> 0 is written, the first element is not sorted. 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 sorting is better than O (N ^ 2), because it exchanges some elements that are far away from each other during a round of sorting.
Although the sorting of each sub-sequence is insert sorting and insertion sorting is stable, the elements of the same size may move in the insertion sorting of different sub-sequences, as a result, the overall stability is broken, so the hill sorting is also unstable.