Takes an integer less than n as the first increment, grouping the sequence. All elements with a multiple of the distance increment are placed in the same group. The direct insert sort is performed in each group first, then the second increment (the second < first) repeats the above groupings and sorts until the increment = 1, where all the elements are placed in the same group for direct insert sorting.
The average initial fetch sequence is half-increment, and then halved each time until the increment is 1.
The following code executes the pass in Nodejs.
functionshellinsertsort (elements, di) {//starting at the position where the increment is located for(vari = di; i < elements.length; i++){ //Ascending if(Elements[i] < elements[i-di]) { //Remove an element of the increment position as an inserted element (Sentinel) varGuard =Elements[i]; varj = i-di; Elements[i]=Elements[j]; //forward, comparing the position of multiples of the increment with the same group and making a direct insertion method while(J >= 0 && guard <Elements[j]) {Elements[j+di] =Elements[j]; J-=di; } //InsertElements[j + di] =Guard; } }}functionShellsort (elements) {//increment for half of the sequence varDi = parseint (elements.length/2); while(di >= 1) {shellinsertsort (elements, DI); //halve each time, the last increment must be 1Di = parseint (DI/2); }}varelements = [10, 9, 8, 7, 6, 5];console.log (' Before: ' +elements); Shellsort (elements); Console.log (' After: ' + elements);
Efficiency: Faster than direct insertion method. But not a stable sorting algorithm, the key depends on the increment of the choice, the first time usually selected half of the sequence length.
(reprint): The Nether of hill sort time complexity is n*log2n. Hill sort does not have fast sorting algorithm fast O (n (logn)), so the medium size is good and the size of a very large data sort is not the best choice. But the algorithm is much faster than the O (n^2) complexity. And the hill sort is very easy to implement, the algorithm code is short and simple. In addition, the hill algorithm does not have much difference in the worst case and average execution efficiency, while fast sequencing performs poorly in the worst case scenario. Experts advocate that almost any sort of work at the beginning can be sorted by hill, and if it proves fast enough in practice, it will be changed to a more advanced sorting algorithm such as fast sorting. In essence, the Hill sort algorithm is an improvement of the direct insertion sorting algorithm, which reduces the number of copies and is much faster. The reason for this is that when the N value is large, the data items need to be sorted for a small number of times, but the data items are very long distances. When the n value decreases, each trip needs and moves more data, which is already close to their final position after sorting. It is the combination of these two situations that makes the hill sort efficiency much higher than the insert sort.
In the beginning of the hill, the increment is larger, more groups, fewer records per group, so the direct insertion within the group is faster, and then the incremental 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 row order, so that the file is closer to the orderly state, So the new trip sort process is also faster.
Insert Sort---Hill insert sort algorithm (JavaScript version)