Algorithm-Hill Sort (C)

Source: Internet
Author: User

/*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&LT;D1), repeat the grouping and sorting work, and so on Until the increment di=1 (DI&LT;...&LT;D2&LT;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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.