The order of the ER (Shell sort) is a sort of insertion. Also called narrowing incremental ordering, is a more efficient version of the direct insertion sort algorithm. Hill sort is a non stable sort algorithm. The method is due to a DL. The shell was named after it was introduced in 1959. Hill sort is to the record by a certain increment of the subscript group, the use of a direct insertion sort algorithm for each group; As the increment gradually decreases, each group contains more and more keywords, when the increment is reduced to 1 o'clock, the whole file is divided into a group, the algorithm will terminate. Hill sort is an unstable sort.
Basic IdeasFirst, take a integer d1 less than n as the first increment, grouping all the records of the file. All records with a multiple distance of D1 are placed in the same group. A direct insertion sort is performed within each group, and then the second increment d2<d1 repeats the grouping and sorting above until the incremental =1 (<. <d2<d1) is taken, where all records are placed in the same group for direct insertion sort.
Example: There is a sequence: 4, 9, 7, 1, 3, 8, 2, 0
The increment is half of the sequence length, or 4, and the sequence is divided into 4 subsequence a,b,c,d;
The four sequences are sorted separately, followed by the following columns: 3, 8, 2, 0, 4, 9, 7, 1, 4, 2, and the sequence into two sub sequences a,b
Sort two subsequence sequence 2, 0, 3, 1, 4, 8, 7, 9
And then take the increment of 2, or 1, to sort the entire sequence to the final sort result
0, 1, 2, 3, 4, 7, 8, 9
Java code implementation:
public static void Shellsort (int[] Data {
//len array length
int len = data.length;
G incremental Initial increment half of the array length for
(int g = len/2;g>0;g/=2) {
for (int i=g;i<len;i++) {
//temporary Save current value
int TMP =data[i];
int j = i-g;
If the current value is small and all previous values in the sequence are compared, the most total determines the correct position of the current value in the sequence while
(J>=0&&data[j] > tmp) {
data[j+g]=data[j] ;
j-=g;
}
Data[j+g]=tmp
}}
}