Hill sorting is one of insert sorting. The insertion sorting algorithm mentioned in the previous article (click to View Details) should be called "directly insert sorting algorithm ", the hill sorting algorithm introduced here is an improved direct insertion sorting algorithm, which was proposed by d.l shell in 1959, it is also called an incremental sorting algorithm.
Basic Ideas
Set the initial sequence to n elements. Select an integer gap less than N or equal to 1 as the interval and divide all elements into gap subsequences, all elements with a distance of gap are placed in the same sub-sequence, and direct insertion algorithms are used in each sub-sequence for sorting. Then, the gap is reduced, for example, Gap = gap/2, repeat the sub-sequence division and sub-sequence sorting actions above until the last gap = 1 is removed and all the elements are put into a sequence.
Simple Example
Initial Sequence: 21 25 49 25 16 8
Step 1:
Step 2:
Step 3:
Algorithm Description
At the beginning, the gap value is large, the sub-sequence contains fewer elements, and the sorting speed is relatively fast. As the sorting progresses, the gap value gradually decreases and the number of elements in the sub-sequence increases, however, the sorting speed is still relatively fast due to the fact that most elements have been sorted in basic order.
There are many methods to obtain the gap value. Initially, shell proposed to take Gap = n/2, Gap = gap/2 until Gap = 1. Later, knuth proposed to take Gap = gap/3 + 1. In addition, some people think that gap is better to go to odd numbers, and some people say that it is better to make the gap values quality. In addition, the example above shows that the algorithm is unstable.
Algorithm Analysis
For a specific sequence to be sorted, You can accurately estimate the number of comparisons and moving times of the elements. However, to determine the dependency between the number of comparisons and moving times of the elements and the incremental selection, and give a complete mathematical analysis, no one can do it. In addition, the example above shows that the algorithm is unstable.
Algorithm Implementation
C language:
# Include <stdio. h> # include <stdlib. h> # include <time. h> # define max 10 # define swap (x, y) {int t; t = x; X = y; y = T;} void shellsort (INT []); int main (void) {int number [Max] = {0}; int I; srand (Time (null); printf ("Before sorting :"); for (I = 0; I <Max; I ++) {number [I] = rand () % 100; printf ("% d", number [I]);} shellsort (number); Return 0;} void shellsort (INT number []) {int I, J, K, Gap, T; Gap = max/2; while (GAP> 0) {for (k = 0; k <gap; k ++) {for (I = K + gap; I <Max; I ++ = gap) {for (j = I-gap; j> = K; j-= gap) {If (number [J]> Number [J + Gap]) {swap (number [J], number [J + Gap]);} else break ;}} printf ("\ NGAP = % d:", GAP ); for (I = 0; I <Max; I ++) printf ("% d", number [I]); printf ("\ n "); GAP/= 2 ;}}
Java language:
public class ShellSort { public static void sort(int[] number) { int gap = number.length / 2; while(gap > 0) { for(int k = 0; k < gap; k++) { for(int i = k+gap; i < number.length; i+=gap) { for(int j = i - gap; j >= k; j-=gap) { if(number[j] > number[j+gap]) { swap(number, j, j+gap); } else break; } } } gap /= 2; } } private static void swap(int[] number, int i, int j) { int t; t = number[i]; number[i] = number[j]; number[j] = t; }}
The end!