Recently looking for a job, do a pen test to sort algorithm, revisit Hill sort, the first time in the look of the times did not understand ... ⊙﹏⊙ b Khan So put yourself in the first time when the code encountered problems to be sorted out, in order to avoid forgetting, must not forget again.
The hill sort (narrowing increment method) is a sort of insert class, which divides the whole sequence into small sequences to be inserted. Hill sort is not stable, O (1) extra space, the time complexity of O (n (logn) ^2). In the worst case, the efficiency of execution is not much different from the efficiency of execution in the average case.
Hill sort interval Sequence function h = h * 3+ 1
The reason Hill sort is much faster than the insertion sort: When the H value is very large, data items the number of elements moving each trip is small, but the distance is very long, this is very efficient; when the H value decreases, each trip sort moves the number of elements, but at this point the data items are close to their final sort position, Insert sort can be more efficient
Directly attached code
public class Shellsort {static void sort (int[] array) {int., IN, TMP;
int len = Array.Length;
int h = 1;
while (H < LEN/3)//Compute interval h maximum h = h * 3 + 1; while (H > 0) {//can continue to split data columns by narrowing the interval h/* out why start with H.
The first subsequence you divide should be such a sequence, 0, H, 2h, 3h, ... * insert sort the while loop starting from 1, because the first number is always ordered, do not need to compare, this need to understand the algorithm of insertion sort, so the comparison is from the second data line, is the first of the array H subscript start
* Out of the decision why is out < Len. * Control array subscript, here's an example that says * * Suppose there is an array of 10 data items, the array subscript from 0 ~ 9 indicates * when the sub sequence of H = 4 o'clock is the case, the following is marked * (0 4 8) (1
5 9) (2 6) (3 7) * It's the first time I've ever understood that it is true to insert each group separately (of course this can be done, but the subscript is not easy to control), but for the following code it is a mistake to understand. * The correct process is this, the outer for loop inserts the first two entries for each group each time, then the first 3, then the top 4 ... This is related to the number of substrings. * The sort process is only true for each other bracket * when out = 4 o'clock for the following process ([0 4] 8) * When out = 5 o'clock ([1 5] 9) * When out = 6 o'clock ([2 6]) * When out = 7 O'Clock ([3 7]) * When out = 8 o'clock ([0 4 8]) * * When out = 9 o'clock ([1 5 9]) * H = 4 execution complete, then H = (h-1)/3 = 1 start new for loop * H = 1 o'clock Execute Process and H = 4 o'clock, but at this time the subsequence is the original sequence, metamorphosis into a simple insertion sort, which is the basic order of the array, the number of data items moved greatly reduced * * */for (out = h;Out < Len;
out++) {//The outer layer determines the second data entry for each set of insertion sorts by out//The following code is the Insert sort algorithm for the subsequence (TMP = array[out];
in = out; * * Compare the insertion of a sort while loop, where the while loop is related to H, so the decision is related to H, including in-= h statements * while (in > 0 && array[in-1] > tmp)
{* Array[in] = array[in-1];
* in--;
*} * array[in] = tmp;
* */while (in > H-1 && array[in-h] >= tmp) {array[in] = array[in-h];
In-= h;
} Array[in] = tmp;
for (int i = 0; i < len; i++)//System.out.print (Array[i] + "");
System.out.println ();
}//Reduce interval H = (h-1)/3; }
}
}