Link: http://blog.csdn.net/morewindows/article/details/6668714
The essence of hill sorting is grouping insertion sorting. This method is also called to narrow down incremental sorting because DL. Shell was proposed in 1959.
The basic idea of this method is to divide the entire sequence of elements to be arranged into several sub-sequences (composed of elements separated by an increment) for direct insertion and sorting, respectively, then, the incremental data is reduced in sequence and then sorted. When the elements in the entire sequence are basically ordered (the increment is small enough), all the elements are directly inserted and sorted. Because the efficiency of direct insert sorting is very high when the elements are basically ordered (close to the best condition), the time efficiency of hill sorting is much higher than that of the first two methods.
Take an array of n = 10, 49, 38, 65, 97, 26, 13, 27, 49, 55, 4 as an example.
First time gap = 10/2 = 5
49 38 65 97 26 13 27 49 55 4
1A 1b
2a 2B
3A 3B
4A 4B
5A 5b
1a, 1b, 2a, and 2B are group tags. The same number indicates that the group is in the same group, and the uppercase letter indicates the first element of the group, insert and sort data in the same group at a time. That is, it is divided into five groups (49, 13) (38, 27) (65, 49) (97, 55) (26, 4) so that after each group is sorted, it becomes (13, 49) (27, 38) (49, 65) (55, 97) (4, 26), the same below.
Second Gap = 5/2 = 3
After sorting
13 27 49 55 4 49 38 65 97 26
1A 1B 1C 1D 1e
2a 2B 2C 2D 2e
Third time gap = 3/2 = 1
4 26 13 27 38 49 55 97 65
1A 1B 1C 1D 1E 1f 1G 1 h 1i 1j
The fourth time gap = 1/2 = 0 is sorted to get the array:
4 13 26 27 38 49 49 55 65 97
The following describes the sort of hill written in strict accordance with the definition.
Void shellsort1 (int A [], int N) {int I, j, gap; For (Gap = n/2; Gap> 0; Gap/= 2) // step size for (I = 0; I <gap; I ++) // Insert the sort directly {for (j = I + gap; j <n; j + = gap) if (A [J] <A [J-Gap]) {int temp = A [J]; int K = J-gap; while (k> = 0 & A [k]> temp) {A [K + Gap] = A [k]; k-= gap ;} A [K + Gap] = temp ;}}}
Obviously, the above shellsort1 code is helpful for an intuitive understanding of hill sorting, but the code size is too large and concise and clear. Therefore, the following improvements and optimizations are carried out. Taking the second sorting as an example, it turns out that each time from 1A to 1E, from 2a to 2E, we can change it to start from 1b and compare it with 1a first, then compare 2B with 2a, and then compare 1C with the data in the previous group ........ Each time, starting from the Gap Element in the array, it is clear that each element is directly inserted and sorted with the data in its own group.
Void shellsort2 (int A [], int N) {Int J, gap; For (Gap = n/2; Gap> 0; Gap/= 2) for (j = gap; j <n; j ++) // starting from the nth Gap Element of the array if (a [J] <A [J-Gap]) // insert and sort each element directly with the data in the group. {int temp = A [J]; int K = J-gap; while (k> = 0 & A [k]> temp) {A [K + Gap] = A [k]; k-= gap ;} A [K + Gap] = temp ;}}
Then, insert the directly inserted sorting part and use the second of the Classical Vernacular algorithm series to directly Insert the sorting part to the three implementations. The third method of directly inserting the sorting is rewritten as follows:
void shellsort3(int a[], int n){ int i, j, gap; for (gap = n / 2; gap > 0; gap /= 2) for (i = gap; i < n; i++) for (j = i - gap; j >= 0 && a[j] > a[j + gap]; j -= gap) Swap(a[j], a[j + gap]);}
In this way, the Code becomes very concise.
Note: The step size of the hill sorting process starts from n/2, and is halved each time until the last step is 1. In fact, you can also have another more efficient step selection, if you are interested in understanding, please refer to Wikipedia on the hill sort step description: http://zh.wikipedia.org/wiki/%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F
Supplemental step selection:
The best known step serial is proposed by Sedgewick (1, 5, 19, 41,109 ,...), the serial number comes from 9*4 ^ I-9*2 ^ I + 1 and 4 ^ I-3*2 ^ I + 1. this study also showed that "comparison is the most important operation in the hill sorting, not the exchange." In this way, the serial sort by step is faster than the insert sort and Heap Sort, and even faster than the fast sort in small arrays. However, when a large amount of data is involved, the hill sort is slower than the fast sort.
Another step-size serial algorithm that performs well in a large array is (the Fibonacci series removes the power of 0 and 1 to calculate the number of the remaining several times the ratio of the Golden partition): (1, 9, 34,182,836,402 5, 19001,903 58, 428481,203 4035, 9651787,458 06244, 217378076,103 16.013 ,...)[2]
Supplemented complexity:
| Step serial |
Worst Case complexity |
|
|
|
|
|
|
(From Wikipedia)
Note:
To help more people understand the image, I add the following figures:
Http://hi.baidu.com/gsgaoshuang/item/17a8ed3c24d9b1ba134b14c2
Assume that the length of the array is 10, and the array elements are: 25, 19, 6, 58, and.
The process of the entire hill sorting algorithm is as follows:
Is the raw data and the incremental D selected for the first time = 5. The result of this sorting is as follows:
This is the result of the first sorting. the incremental value of this selection is d = 2. The result of this sorting is as follows:
When d = 1 is the last sorting, This sorting is equivalent to a loop of Bubble sorting. The final result is as follows:
I guess everyone can understand this picture.
Next is the source code time:
Click Download source code