First, the forefront:
The name of the hill sort (Shell sort) originates from its inventor Donald Shell, which is one of the algorithms that broke through the two-dimensional time barrier. It works by comparing elements that are spaced apart, and the distances used by the trips are reduced as the algorithm progresses, until only the last pass of the adjacent elements is compared, so the hill sort is sometimes called a reduced incremental sort (diminishing increament sort).
Second, detailed steps:
Hill sort uses a sequence H1,H2,...... HT is called an incremental sequence, as long as the h1=1, any increment sequence is feasible, after using the one-way increment sequence of HK, any a[i]<=a[i+k in the array is meaningful. All elements separated by K are ordered, and this is called the array is HK ordered. Sort the situation after each trip for Hill:
The general practice of the HK sort is that for each position I in hk,hk+1.......n-1, place the elements on the I,I-HK,I-2HK, ... In the correct position on the Although it does not affect the final result, it can be found through observation that the role of a trip to HK is to perform an insertion order on the HK separate sub-array, which is important when analyzing the run time of the hill sort!!
Third, the implementation of the algorithm
In the question of the incremental sequence, you can pass HT=[N/2] and HK=[HK+1/2]. You can also do this by setting up an increment sequence yourself:
1 /**2 * Hill Sort is an improved version of the simple insert sort, breaking through the complexity of O (n*n) sorting algorithm, which differs from the insertion sort is3 *, it takes precedence over elements that are farther apart, it's called narrowing the incremental sort4 */5 Public Static voidShellSort1 (int[] R,intLowintHighint[] Delta) {6 for(intk = 0; K < Delta.length; k++)7Shellinsert (R, Low, High, delta[k]);//Direct Insert sort with step length of delta[k]8 }9 //Shellinsert performs an insert sortTen Private Static voidShellinsert (int[] R,intLowintHighintDeltak) { One for(inti = low + Deltak; I <= high; i++) A if(r[i]<r[i-Deltak]) { - //less than, you need to insert r[i] into the ordered table, the process of execution is the same as the insertion sort - inttemp =R[i]; the intJ; - for(j= I-deltak; j >= Low && temp < r[j]; j = J-Deltak) -R[j + Deltak] = R[j];//record and move back -R[j + Deltak] = temp;//Insert to correct position + System.out.println (arrays.tostring (R)); - } + A at}
Iv. Analysis of time complexity
The run time of the hill sort depends on the selection of the incremental sequence, and the average case analysis of the hill sort, in addition to some of the most mundane incremental sequences, is a long-term unsolved problem. We'll sort the worst case analysis for Hill:
1. The worst case scenario for Hill sequencing when using hill increments is O (n2)
2. The worst case run time for hill sequencing using Hibbard increments is O (N3/2): proof slightly
In practice, most of the hill sort runs at O (N3/2)
V. References
Analysis of data structure and algorithms
Insert sort algorithm of hill sort