1. Basic Ideas
Hill sort is to group records by a certain increment of the subscript, sorting each group using the direct insertion sorting algorithm; As the increments gradually decrease, each group contains more and more keywords, when the increment is reduced to 1 o'clock, the entire file is divided into a group, the algorithm terminates.
2. Principle of Implementation
For N ordered series, take an integer less than n (Gap is called step) to divide the ordered elements into groups of sub-sequences, all records with multiples of gap are placed in the same group, and then the elements within each group are directly inserted into a sort. Once this sequence is complete, the elements of each group are ordered. The gap value is then reduced, and the grouping and sorting are performed repeatedly. Repeat this operation, when the gap=1, the whole sequence is orderly.
3. Code Examples
(1) Code:
/*** Sort a single group in the hill sort * <p> * parameter Description: * A--array to be sorted * N--The total length of the array * I--the starting position of the group * gap--Group The Step size * <p> * Group is "Starting from I, will be separated by the number of gap length" of the composition! */ Public Static voidGroupsort (int[] A,intNintIintGap) { //Print GroupingSystem.out.print ("gap=" +gap+ "["); //to print the first element of a comparisonSystem.out.print (A[i]); //Direct Insert Sort for(intj = i + gap; J < N; J + =Gap) { //elements to be comparedSystem.out.print ("" +A[j]); //if A[J] < A[j-gap], look for the a[j] position and move the position of the trailing data back. if(A[j] < a[j-Gap]) { intTMP =A[j]; intK = J-Gap; while(k >= 0 && A[k] >tmp) {A[k+ Gap] =A[k]; K-=Gap; } a[k+ Gap] =tmp;//System.out.print ("+a[j]+" <--> "+a[j-gap]+");}} System.out.println ("]"); } /*** Hill Sort * <p> * parameter Description: * A--array to be sorted * N--The length of the array*/ Public Static voidShellsort (int[] A,intN) {//gap is the step size, each reduced to the original half. for(intGap = N/2; Gap > 0; Gap/= 2) { //A total of gap groups, performing a direct insert sort for each group for(inti = 0; I < gap; i++) Groupsort (A, n, I, GAP); System.out.printf ("After sort:"); for(inti = 0; i < a.length; i++) System.out.printf ("%d", A[i]); System.out.println (); System.out.println (); } } Public Static voidMain (string[] args) {inti; intA[] = {80, 30, 60, 40, 20, 10, 50, 70,100}; System.out.printf ("Before sort:"); for(i = 0; i < a.length; i++) System.out.printf ("%d", A[i]); System.out.println (); System.out.println (); Shellsort (A, a.length); }
(2) Results:
Before sort:80 30 60 40 20 10 50 70 100
gap=4 [80 20 100]
gap=4 [30 10]
gap=4 [60 50]
gap=4 [40 70]
After sort:20 10 50 40 80 30 60 70 100
gap=2 [20 50 80 60 100]
gap=2 [10 40 30 70]
After sort:20 10 50 30 60 40 80 70 100
Gap=1 [20 10 50 30 60 40 80 70 100]
After Sort:10 20 30 40 50 60 70 80 100
4. Algorithm Analysis
(1) Hill Sort time complexity
The time complexity of hill sorting is related to the selection of increments (i.e., step Gap). For example, when the increment is 1 o'clock, the hill sort degenerates into a direct insert sort, where the time complexity is O (N²), while the Hibbard increment of the hill sort has a time complexity of O (N3/2).
(2) Hill sort space complexity
The spatial complexity is O (1) because there is only one buffer unit.
(3) Hill sort stability
Hill sequencing is an unstable algorithm that satisfies the definition of a stable algorithm. For the same two numbers, they may change in order because they are divided into different groups.
Algorithm stability-assuming that there is a a[i]=a[j in the sequence, A[i] before the order, and A[i] is still in front of a[j before ordering). Then this sort algorithm is stable!
5. Sorting features
We know that once the insertion sort is stable, but in different insertion sorts, the same elements may move in their own insert sort, and finally their stability will be disturbed, so the hill sort is unstable.
The time performance of the Hill sort is better than the direct insert sort for the following reasons:
(1) The number of comparisons and movements required to insert a sort directly when the initial state of the file is basically ordered is less.
(2) When the n value is small, the difference between N and N2 is also small, that is, the best time to insert the order of the complexity O (n) and the worst time complexity 0 (n2) difference is not small.
(3) In the beginning of the hill, the increment is larger, more groups, the number of records per group is small, so the direct insertion within the group is faster, the increment di gradually reduced, the number of groups gradually decreased, and the number of records of the group gradually increased, but because already according to Di-1 as a distance arrangement, so that the file is closer to the So the new trip sort process is also faster.
Therefore, the efficiency of hill sort is better than direct interpolation.
The average time complexity for hill sorting is O (NLOGN).
Sorting algorithm (3)--insert sorting--insertion Sort [3]--shell sort--Hill Sort