1. Basic idea:
Hill sort also becomes "shrinking incremental sort", the basic principle is that the array elements to be sorted are divided into several subsequence, so that the number of elements in each subsequence is relatively few, then the sequence of each subsequence is directly inserted, after the whole backlog sequence is "basically ordered", and then all the elements are immediately inserted into the order. Therefore, we want to adopt the strategy of jumping segmentation: to make a subsequence of a "increment" in order to ensure that the result of the direct insertion in the subsequence is basically orderly rather than local order. Hill sort is the optimization and upgrade of the direct insertion sort algorithm.
The so-called basic order, is the small keyword basic in front, large basic in the back, a moderate basic in the middle, such as {2,1,3,6,4,7,5,8,9,} can be called the basic order. But like {1,5,9,3,7,8,2,4,6}, 9 in third place, 2 in the third place is not basic order. 2, the complexity of the analysis:
Hill sort of the key is not casually grouped after their own sorting, but the interval between an "increment" of the record into a subsequence, to achieve a jump-type movement, so that the efficiency of the ranking improved. It is important to note that the last increment value of the increment sequence must be equal to 1. In addition, Hill sorting is not a stable sort algorithm because the record is a jump-type movement.
Hill ranking the best time complexity and average time complexity are, the worst time complexity of. 3, the sorting process is as follows:
Take the array {26, 53, 67, 48, 57, 13, 48, 32, 60, 50} as an example, the step sequence is {5,2,1}
Initialization keywords: [26, 53, 67, 48, 57, 13, 48, 32, 60, 50]
The final sort result:
4, Java implementation is as follows:
public static void Shellsortsmalltobig (int[] Data {int j = 0;
int temp = 0; for (int increment = DATA.LENGTH/2, increment > 0; increment/= 2) {System.out.println ("increment:" + in
Crement);
for (int i = increment i < data.length; i++) {//System.out.println ("I:" + i);
temp = Data[i];
for (j = i-increment J >= 0; J-= increment) {//System.out.println ("J:" + j);
SYSTEM.OUT.PRINTLN ("Temp:" + temp);
System.out.println ("data[" + j + "]:" + data[j]);
if (temp < data[j]) {data[j + increment] = Data[j];
} else {break;
} data[j + increment] = temp;
for (int i = 0; i < data.length i++) System.out.print (Data[i] + "");} public static void Main (string[] args) {int[] data = new int[] {26, 53, 67, 48, 57, 13, 48, 32, 60,
50};
Shellsortsmalltobig (data);
System.out.println (arrays.tostring (data)); }