Comic: What is Hill sort?



The next day ----- -----







————————————




Let's take a look at insertion sort :


Insertion sort name implies, is the sorting process, to each element of the array in accordance with the magnitude relation, into the corresponding position in front of the ordered regions.


Such as the following elements in the array 3 according to the magnitude relation, the three elements needs to be inserted before the front ordered regions, and the first three elements of the corresponding moving rearwardly:









And so on, will be a total of insertion sort (array length -1) rounds, each round was as follows:



Insertion sort of average time complexity is O (n-2 ^) . This sorting algorithm is not complicated, but obviously not an efficient sorting algorithm.


So, how you can be made to optimize the insertion sort it? We might as well start from the insertion sort of two characteristics:


1. In most of the elements have been ordered, the insertion sort of smaller workload

The obvious conclusion is that, if a majority of the array elements are ordered, then the natural elements in the array does not need to be compared and exchanged frequently.


2. In the case of a small number of elements, insertion sort less work

This conclusion is even more obvious, and n is proportional to the square of the workload insertion sort, if n is small, then the sort of workload naturally much smaller.




How to preprocess the original array it? Scientists think clever method for ordering packets, in order to arrays certain "rough adjustment."



The so-called grouping is to make two hundred twenty-one group elements, with the span between the two groups of elements, it is half of the total length of the array, which is a span of 4.



As shown, element 5 and a set of elements 9, 8 of elements 2 and a set of elements, the elements 6 and a set of element 1, element 3 and a group 7 element, a total of 4 groups.


Next, we let each group elements independent sort, sort by direct insertion sort can be. As the number of elements in each group rarely, and only two, so little insertion sort of workload. After completion of each array sorted as follows:



Thus, after only a few simple exchange, the overall degree of order of the array has been significantly improved, so that the workload for subsequent re-direct insertion sort greatly reduced. This approach can be understood as "rough adjustment" of the original array.


But this also is not finished, we can further narrow the span grouping, repeat the above work. The span is reduced to half the original, which is the span of 2 re-group elements:



As shown, a set of elements 5,1,9,6, 2,3,8,7 a set of elements, a total of two.


Next, we continue to allow each group elements independent sort, sort by direct insertion sort can be. After completion of each array sorted as follows:



In this case, the degree of ordered arrays further increase, paving the way for subsequent sequencing to be performed.


Finally, we span the packet is further reduced, so that the span is 1, it might just make a direct insertion sort. After a series of rough adjustment before, directly into the sort of workload decreased a lot, sort results are as follows:




Let us re-comb the entire process of ordering packets:





Like this gradually packet coarse, and then directly into the sort of thinking that Hill sorting , according to the inventor of the algorithm, the computer scientist Donald Shell named names.


Examples of groups used in the above span (4,2,1), is referred to as the Hill sorting increments , the increment can have a variety of selection, binary stepwise increments in the example we used, by Donald a plain method proposed in the hair Shell sort Mingxi Er, referred Hill increments .




public static void sort(int [] array){    //希尔排序的增量    int d=array.length;    while(d>1) {        //使用希尔增量的方式,即每次折半        d=d/2;        for(int x=0;x<d;x++) {            for(int i=x+d;i<array.length;i=i+d) {                int temp=array[i];                int j;                for(j=i-d;j>=0&&array[j]>temp;j=j-d) {                    array[j+d]=array[j];                }                array[j+d]=temp;            }        }    }}public static void main(String [] args){    int[] array = {5,3,9,12,6,1,7,2,4,11,8,10};    sort(array);    System.out.println(Arrays.toString(array));}复制代码






Look at the above array, grouping ideas before if we copy, whether in 4 increments, or increments of 2, elements within each set without any exchange. We increments until reduced to 1, the array will be adjusted in accordance with the direct insertion sort of way.


For such an array, Hill sorting not only failed to reduce the workload of direct insertion sort, but in vain increasing the cost of group operations.



How to choose a more efficient way of doing things is the Hill incremental sort?


In order to ensure that coarse no blind packet, an increment per one another as needed "prime", i.e. no common divisor other than 1.


So, people have proposed a variety of incremental ways, the most representative is Hibbard incremental and Sedgewick increment .


Hibbard increment sequence is as follows:

1,3,7,15......

Term Formula 2 ^ k-1

The use of such incremental Hill sorting worst time complexity is O (n ^ (3/2))


Sedgewick increment sequence is as follows:

1, 5, 19, 41, 109......

Term Formula 9 * 4 ^ k - 9 * 2 ^ k + 1 or 4 ^ k - 3 * 2 ^ k + 1

The use of such incremental Hill sorting worst time complexity is O (n ^ (4/3))


About time these two incremental complexity, some need very complicated mathematical proof, some people generally suppose, we temporarily do not tangle.



Among the above array, there are two elements 5, 5 of the first green, orange after 5.


If we then grouped Hill increments, the first round of coarse (in increments of 4), the green element 4 will be exchanged with the element 5, to change the back orange 5:



The second round of coarse (in increments of 2) after:



Final ranking results:



The same elements 5, after changing the sort order, we can see is a sort Hill unstable sort .



—————END—————




Like this article, welcome public attention No. programmer small gray , more exciting content to watch



Welcome to the two-dimensional code press attention small gray learn English , you learn not only English!



Guess you like

Origin juejin.im/post/5de087ede51d452515148003