The mountain is the essence of the package insert sort sort. This method is also known as a narrow incremental sort because of DL. Shell to 1959 named.
The basic idea of this method is to first cut the whole sequence of elements into several sub-sequences (consisting of an element separated by an "increment"), make the direct insertion sort separately, then reduce the increment sequentially, then order the elements in the whole sequence (the increment is small enough), then make a direct insertion sort of the whole element.
Because the direct insertion sort is very efficient when the elements are basically ordered (close to the best case), the hill rank is significantly more efficient in time than the first two methods.
An array of n=10 49, 38, 65, 97, 26, 13, 27, 49, 55, 4 for example
First time gap = 10/2 = 5
49 38 65 97 26 13 27 49 55 4
1 a 1B
2 a 2B
3 A 3B
4 a 4 b
5 a 5B
1a,1b,2a,2b are grouped, numbers are represented in the same group, uppercase letters are the first element of the group, and each time the data for the same group is inserted directly into the sort.
It is divided into five groups (49, 13) (38, 27) (65, 49) (97, 55) (26, 4) so that each group is sorted (13, 49) (27, 38) (49, 65) (55, 97) (4, 26), the same as below.
Second time gap = 5/2 = 2
After sorting
13 27 49 55 4 49 38 65 97 26
1 a 1 b 1C 1D 1E
2 a 2B 2C 2D 2E
The third time gap = 2/2 = 1
4 26 13 27 38 49 49 55 97 65
1 a 1 b 1C 1D 1E 1F 1G 1H 1I 1J
Fourth time gap = 1/2 = 0 sorted to get array:
4 13 26 27 38 49 49 55 65 97
The following is a strict definition to write the hill sort
void Shellsort1 (int a[], int n) {int I, J, gap;for (gap = N/2; gap > 0; Gap/= 2)//step for (i = 0; I < gap; i++)
//Direct Insert Sort {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;}}}
Very obvious. The Shellsort1 code above, though helpful for an intuitive understanding of the hill sort, is too large in code. Not concise and clear. As a result, improvements and optimizations are made, taking the second order as an example, from 1A to 1E at a time, from 2A to 2E. Can be changed from 1B to start. First and 1A comparison, then take 2B and 2A comparison, then take 1C and the front of their own group of data comparison .... This starts each time from the gap element of the array. The direct insertion of each element with the data within its own group is clearly correct.
void Shellsort2 (int a[], int n) {int J, gap;for (gap = N/2; gap > 0; Gap/= 2) for (j = Gap; J < N; j + +)//from array of gap elements Start if (A[j] < A[J-GAP])//each element is directly inserted into the sort of data within its own group {int temp = A[j];int k = j-gap;while (k >= 0 && a[k] > t EMP) {a[k + gap] = a[k];k-= gap;} A[k + gap] = temp;}}
And then directly into the sort section with the Vernacular classical algorithm series of two direct insertion in the sort of three implementations directly into the sort of the third method to rewrite the following:
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]);}
This makes the code very concise.
Note: The step selection for the above Hill sort is from N/2, and then halved each time, straight at the end of 1. In fact, it may have another more efficient step to choose from, assuming that the reader is interested in understanding. See description of Shell sequencing steps Wikipedia:
Http://zh.wikipedia.org/wiki/%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F
Three vernacular classic Algorithm series Shell sort implementation