Basic Ideas of hill sorting
Shell sort is also called "downgrading incremental sorting ". It was proposed by D. l. Shell in 1959. The basic idea of this method is to divide the entire sequence of elements to be arranged into several sub-sequences (composed of elements separated by an increment) for direct insertion and sorting, respectively, then, the incremental data is reduced in sequence and then sorted. When the elements in the entire sequence are basically ordered (the increment is small enough), all the elements are directly inserted and sorted. Because the efficiency of direct insert sorting is very high when the elements are basically ordered (close to the best condition), the time efficiency of hill sorting is much higher than that of the first two methods.
Specific practice: first determine a group of incremental D0, D1, D2, D3 ,..., the dt-1 () Where n> D0> d1>...> dt-1 = 1), for I =, 2 ,..., t-1, followed by the following processing: according to the current incremental di divide n elements into di groups, each group of elements subscript is di; insert and sort the elements in each group.
2. The following table lists Hill sorting. Algorithm .
(1) The results of sorting by hill sorting method are as follows:
Initial: 503,17, 512,908,170,897,275,653,426,154,509,612,677,765,703, 94
1st {d1 = 8}: 509,612,170,765,275, 94,503,154,512,908,677,897,703,653
2nd (d2 = 4): 170,17, 275,94, 426,154,509,612,503,765,512,653,677,897,703,908
3rd trip (D3 = 2): 170,17, 275,94, 426,154,503,612,509,653,512,765,677,897,703,908
4th (d1 = 1): 154,170,275,426,503,509,512,612,653,677,703,765,897,908
(2) For example, n = 8. the eight elements of array a are 17,3, 30,25, 14,17, 20,9, respectively.
Shell sorting process for a given instance
Assume that the file to be sorted has 10 records, and the keywords are:
49,38, 65,97, 13.
The incremental sequence values are as follows:
5, 3, 1
The sorting process is shown in [animation simulation demo ].
Shell Sorting Algorithm Implementation
1. Algorithm Description without monitoring record
Using system;
Using system. Collections. Generic;
Using system. text;
Namespace exshellsorter
{
Public class shellsorter
{
Public void sort (INT [] ARR)
{
Int Inc;
For (INC = 1; INC <= arr. Length/9; Inc = 3 * Inc + 1 );
For (; INC> 0; INC/= 3)
{
For (INT I = inc + 1; I <= arr. length; I + = Inc)
{
Int T = arr [I-1];
Int J = I;
While (j> Inc) & (ARR [J-inc-1]> T ))
{
Arr [J-1] = arr [J-inc-1]; // exchange data
J-= Inc;
}
Arr [J-1] = T;
}
}
}
Static void main (string [] ARGs)
{
Int [] array = new int [] {1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
Shellsorter S = new shellsorter ();
S. Sort (array );
Foreach (INT m in array)
Console. writeline ("{0}", M );
}
}
}
Note:
When incremental d = 1, shellpass and insertsort are basically the same, but a cycle criterion "j> 0" is added to the insertsort because there is no Sentel to prevent the subscript from crossing the border.
2. Set the shell Sorting Algorithm for monitoring record
Specific algorithms [bibliography [12]
Algorithm Analysis
1. incremental sequence Selection
The execution time of shell sorting depends on the incremental sequence.
Common Features of a good incremental sequence:
① The last increment must be 1;
② Avoid the mutual multiples of values (especially adjacent values) in the sequence.
A large number of experiments have been conducted to show the current good results: WHEN n is large, the number of comparisons and moves is between nl.25 and 1.6n1.25.
2. Shell sorting has better time performance than direct insertion sorting
The reason why the time performance of hill sorting is better than that of direct insertion sorting:
① When the initial state of the file is basically ordered, the number of comparisons and moves required for direct insertion sorting is relatively small.
② When the N value is small, the difference between N and N2 is also small, that is, the best time complexity of direct insertion sorting O (N) and the worst time complexity 0 (N2) are not much different.
③ At the beginning of the hill sorting, there were a large increase in the number of groups and a small number of records in each group. Therefore, the number of records in each group was rapidly inserted. Later, the incremental di gradually reduced and the number of groups gradually reduced, the number of records in each group gradually increased, but because the di-1 has been used as the distance sorting order, the file is closer to the orderly state, so the new sorting process is also faster.
Therefore, the efficiency of hill sorting is much higher than that of direct insertion sorting.
3. Stability
Hill sorting is unstable. For more information, see the preceding example. In this example, the relative order of two identical keywords 49 changes before and after sorting.