/*shellsort.c-by Chimomo Hill Sort, also known as "narrowing incremental sorting", is an improvement to the direct insertion sorting method. The basic idea of hill sort is that the whole order sequence is divided into several sub-sequences, then the direct insertion sort is done separately, and then the whole record is sorted by a direct insertion when the records in the entire sequence are basically ordered. The practice is to take an integer less than n D1 as the first increment, all the records with a D1 multiple are placed in the same group, so that all records are divided into D1 groups, directly inserted in the group, and then take the second increment D2 (D2<D1), repeat the grouping and sorting work, and so on Until the increment di=1 (DI<...<D2<D1) is taken, that is, all records are placed in the same group for direct insert sorting. The hill sort is an unstable sort method. */void shellsort (int data[], int n) {int *delta, k, I, T, DK, J; /* starting from k = N, repeat k = K/2 operation until k = 0, the sequence of k values is stored as an incremental sequence delta*/k = n; Delta = (int *) malloc (sizeof (int) * (N/2)); i = 0; do {k = K/2; delta[i++] = k; } while (k > 0); i = 0; while ((DK = delta[i]) > 0) {for (k = delta[i]; k < n; ++k) {/* element Data[k] is inserted into the ordered Delta Sub-table */ if (Data[k] < DATA[K-DK]) {/* Back up the element to be inserted, empty an element position */t = data[k]; for (j = k-dk; J >= 0 && T < Data[j]; J-= DK) {/* Find simultaneous element at insertion position Move back */data[j + DK] =DATA[J]; }/* Find the insertion position, insert element */data[j + DK] = t; }}/* Remove an increment value */++i; }}
/*main.c-by Chimomo*/main () {int I, a[] = {$, Notoginseng, 0, 3,--------------------;p rintf ("Original array:\n"); I < 10; i++) {printf ("%d", A[i]);} printf ("\ n"); Shellsort (A, ten);p rintf ("Shell sorted array:\n"); for (i = 0; i < i++) {printf ("%d", A[i]);} printf ("\ n");}
Algorithm-Hill Sort (C)