Hill sort , also called descending incremental sorting algorithm , is a more efficient and improved version of insertion sequencing. Hill Sort is a non-stable sorting algorithm.
The hill sort is based on the following two-point nature of the insertion sort, which proposes an improved method:
- The insertion sort is efficient in the case of almost sequenced data operations, i.e. the efficiency of linear sequencing can be achieved
- But the insertion sort is generally inefficient because the insertion sort can only move data one at a time
The original algorithm implementation requires O (n2) comparisons and exchanges in the worst case scenario. V. Pratt's book has made minor modifications to the algorithm, allowing performance to be increased to O (nlog2n). This is worse than the best comparison algorithm O (nlogn).
Hill sort improves the performance of the insert sort by dividing all the elements of the comparison into several areas. This allows an element to move one step at a time toward the final position. The algorithm then takes the smaller step to sort, the final step of the algorithm is the normal insertion sort, but in this step, the data to be sorted is almost lined up (the insertion sort is faster).
Suppose there is a very small data at the end of an array that has been sorted in ascending order. If you use a sort of complex O (N2) (bubble sort or insert sort), you might compare and swap n times to move the data to the correct location. The hill sort moves the data in a larger stride size, so small data is only a few comparisons and exchanges to the right place.
A better understanding of the hill sort implementation: The array is listed in a table and the columns are sorted (sorted by insertion). Repeat the process, but do it with a longer column at a time. Finally, there is only one column for the entire table. Converting an array to a table is to better understand the algorithm, which itself simply sorts the original array (by increasing the step size of the index, for example, instead i += step_size i++ ).
For example, suppose you have a group of numbers [13 14 94 33 82 25 59 94 65 23 45 27 73 25 39 10], and if we start with a step of 5, we can better describe the algorithm by placing the list in a table of 5 columns, so they should look like this:
13 14 94 33 8225 59 94 65 2345 27 73 25 3910
Then we sort each column:
10 14 73 25 2313 27 94 33 3925 59 94 65 8245
By connecting the four lines together, we get: [10 14 73 25 23 13 27 94 33 39 25 59 94 65 82 45]. Then 10 is moved to the correct position, and then the step is sorted by 3:
10 14 7325 23 1327 94 3339 25 5994 65 8245
After sorting becomes:
10 14 1325 23 3327 25 5939 65 7345 94 8294
The final sequence is sorted in 1 steps (this is a simple insert sort).
#include <stdio.h>#include<stdio.h>voidShellsort (int*a,intN) { intI, J, K, T; K= N/2; while(k >0) { for(i = k; i < n; i++) {T=A[i]; J= i-K; while(J >=0&& T <A[j]) {A[j+ K] =A[j]; J= J-K; } a[j+ K] =T; } k/=2; }}intMain () {intA[] = {8,Ten,3,5,7,4,6,1,9,2}; intN; N=sizeof(a)/sizeof(a[0]); Shellsort (A, N); for(intK =0; K < N; k++) printf ("a[%d] =%d\n", K,a[k]); return 0;}
Http://www.cnblogs.com/archimedes/p/4015599.html
Data structure--sort--Hill sort algorithm