Hill sort (Shell ' s sort) is a very "magical" sort algorithm. It's "magical" because no one can tell exactly what its performance is. Hill Sort by DL. The shell was named after it was introduced in 1959. Since the C. A. R. Hoare in 1962, after a quick sort, because of its simpler, the general use of fast sorting. Many mathematicians, however, have been tireless in their search for the best complexity of Hill's ranking. As an ordinary programmer, we can learn the idea of Hill.
By the way, before Hill sort appeared, there was a general notion that the "sort algorithm could not break through O (N2)" in the computer world. Hill sort of appeared to break the spell, and soon, fast-sequencing algorithms came out successively. In this sense, hill sort leads us to a new era.
Algorithm Overview/Ideas
Hill sort of the proposed, mainly based on the following two points:
1. The insertion sort algorithm can approximate O (n) complexity and is highly efficient in the case of an array in a basically orderly manner.
2. But the insertion sort can only move data one bit at a time, and performance can deteriorate rapidly when the array is large and basically unordered.
Based on this, we can use a grouped insertion sort method, as in the case of an array of 16 element sizes.
1. Select an delta delta, which is greater than 1, and select the sub array by this increment from the array for a direct insert sort. For example, if you select an increment of 5, the elements that are subscript 0,5,10,15 are sorted.
2. Keep the delta Delta and move the first element in turn to insert the order directly until the round completes. For the above example, the array [1,6,11],[2,7,12],[3,8,13],[4,9,14] is sorted sequentially.
3. Reduce the increment and repeat the process until the increment is reduced to 1. Obviously, the last time for direct insertion sort.
4. Sorting completed.
As you can see from the above, the increment is decreasing, so the hill sort is reduced to "incremental sort."
Code implementation
Package sort;
public class Shellsorttest {public static int count = 0;
public static void Main (string[] args) {int[] data = new int[] {5, 3, 6, 2, 1, 9, 4, 8, 7};
print (data);
Shellsort (data);
print (data);
The public static void Shellsort (int[] Data {//) computes the largest h-value int h = 1;
while (H <= data.length/3) {h = h * 3 + 1;
while (H > 0) {for (int i = h; i < data.length i + = h) {if (Data[i] < data[i-h]) {
int tmp = Data[i];
int j = i-h;
while (J >= 0 && data[j] > tmp) {data[j + h] = data[j];
J-= h;
} data[j + h] = tmp;
print (data);
///Calculate the next h value h = (h-1)/3; } public static void print (int[] Data {for (int i = 0; i < data.length; i++) {System.out.print
(Data[i] + "T");
} System.out.println (); }
}
Run Result:
5 3 6 2 1 9 4 8 7 1 3 6 2 5 9 4-8 7
1 2 3 6 5 9 4 8 7 1 2 3 5-6 9 4 8 7
1 , 2 3 4 5 6 9 8 7 1 2 3 4 5 6 8 , 9 7 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
Algorithm Performance/Complexity
the incremental sequence of hill sort can be taken, and the only condition required is that the last one must be 1 (because it is guaranteed to be ordered by 1). However, different sequence selection will greatly affect the performance of the algorithm. The above code shows two increments.
Remember: It is best not to have a common factor other than 1 for every two elements in an incremental sequence! (Obviously, it's not very important to sort by a sequence of 4 and then 2.)
The following are some common incremental sequences.
The first increment was the initial increase of the Donald Shell, that is, the binary down until 1. According to the study, the time complexity of using hill increment is still O (N2).
The second increment hibbard:{1, 3, ..., 2^k-1}. The time complexity of the increment sequence is approximately o (n^1.5).
The third increment Sedgewick increment: (1, 5,,, 109,...), the generation sequence is either 9*4^i-9*2^i + 1 or 4^i-3*2^i + 1.
Algorithm stability
we all know that the insertion sort is the stability algorithm. However, the shell sort is a process that is inserted multiple times. In one insert we can ensure that the order of the same elements is not moved, but in multiple inserts the same element is completely likely to be moved at different insertion rounds and the last stability is destroyed, so the shell sort is not a stable algorithm.
The
algorithm applies to scenarios
the shell sort is fast, but after all, it is the insertion sort, whose order of magnitude does not have an Up-and-comer-fast sort O (n㏒n) fast. In the face of a lot of data, shell ordering is not a good algorithm. However, small and medium sized data can be used entirely.