Insert sort directly-insert semi-insert sort 2-insert sort by hill

Source: Internet
Author: User
/* Function, data structure C language version of the P265-P272, direct insertion of sorting-semi insertion of sorting-2 insert sort-hill insertion of sorting blog, Hangzhou Saturday, January 5, 2013 */# include <stdio. h> # include <malloc. h> typedef int keytype; // defines the type of the keyword as integer typedef int infotype; // defines the type of other data items // record type typedef struct {keytype key; // Keyword: infotype otherinfo; // other data items} redtype; // maximum length of a small sequence table used as an example # define maxsize 20 // sequence table Type typedef struct {redtype R [Maxsize + 1]; // R [0] idle or used as the int length of the Sentinel unit; // sequence table length} sqlist; // print the sequence table void print (sqlist L) {int I; for (I = 1; I <= L. length; I ++) printf ("(% d, % d)", L. R [I]. key, L. R [I]. otherinfo); printf ("\ n");} // algorithm 10.1 p265 // insert and sort the sequence table L directly. Void insertsort (sqlist * l) {int I, j; // non-descending order for (I = 2; I <= (* l ). length; ++ I) {If (* l ). R [I]. key <(* l ). R [I-1]. key) {// copy to Sentinel (* l ). R [0] = (* l ). R [I]; // record postshift for (j = I-1; (* l ). R [0]. key <(* l ). R [J]. key; -- j) (* l ). R [J + 1] = (* l ). R [J]; // insert to the correct position (* l ). R [J + 1] = (* l ). R [0]; print (* l) ;}}// algorithm 10.2 p267 // performs semi-insert sorting on the sequence table L. Void binsertsort (sqlist * l) {int I, j, M, low, high; for (I = 2; I <= (* l ). length; ++ I) {// set L. R [I] is saved to L. R [0] (* l ). R [0] = (* l ). R [I]; Low = 1; high = I-1; // in R [low .. high] In the left half to find the position of the ordered insert while (low <= high) {M = (low + high)/2; // fold half if (* l ). R [0]. key <(* l ). R [M]. key) High = s-1; // less than the insertion point in the lower half zone elselow = m + 1; // other insertion points in the upper half zone} // record the post shift for (j = I-1; j> = high + 1; -- j) (* l ). R [J + 1] = (* l ). R [J]; // insert (* l ). R [high + 1] = (* l ). R [0]; print (* L) ;}}// 2-way insertion sorting p267void p2_insertsort (sqlist * l) {int I, j, first, final; redtype * D; // generate L. length record temporary space d = (redtype *) malloc (* l ). length * sizeof (redtype); // set the 1st records of L to the records in the order of D (in the position [0]) d [0] = (* l ). R [1]; // first and final indicate first = final = 0 for the first and last records of the sorted records in D; for (I = 2; I <= (* l ). length; ++ I) {// sort the 2nd ~ Insert the last record to If (* l) in D ). R [I]. key <D [first]. key) {// The value of the record to be inserted is smaller than the minimum value in D. before inserting the record to d [first] (Elements of the D array do not need to be moved) First = (first-1 + (* l ). length) % (* l ). length; // set D as the circular vector d [first] = (* l ). R [I];} else if (* l ). R [I]. key> d [Final]. key) {// The value of the record to be inserted must be greater than the maximum value in D. After inserting the record to d [Final] (Elements of the D array do not need to be moved) Final = final + 1; d [Final] = (* l ). R [I];} else {// The value of the record to be inserted must be greater than the minimum value in D, smaller than the maximum value in D, and inserted to the middle of D (the elements of D array need to be moved) j = final ++; // move the tail element of D to insert the record while (* l) in sequence ). R [I]. key <D [J]. key) {d [(j + 1) % (* L ). length] = d [J]; j = (J-1 + (* l ). length) % (* l ). length;} d [J + 1] = (* l ). R [I] ;}/// assign d to L. r, linear relationship for (I = 1; I <= (* l ). length; I ++) (* l ). R [I] = d [(I + first-1) % (* l ). length];} // algorithm 10.4 p272 // insert and sort the sequence table L with a slash. This algorithm makes the following changes compared with directly inserted sorting: // 1. the increment of the front and back record locations is DK, not 1; // 2.r[ 0] is only a temporary storage unit, not a sentry. When j <= 0, the insert position is found. Void shellinsert (sqlist * l, int DK) {int I, j; for (I = Dk + 1; I <= (* l ). length; ++ I) {If (* l ). R [I]. key <(* l ). R [I-dk]. key) {// (* l ). R [I] inserts an ordered incrementing quantum table and stores it in (* l ). R [0] (* l ). R [0] = (* l ). R [I]; // move the record back to find the insert position for (j = I-DK; j> 0 & (* l ). R [0]. key <(* l ). R [J]. key); j-= dk) (* l ). R [J + dk] = (* l ). R [J]; (* l ). R [J + dk] = (* l ). R [0]; // insert }}// algorithm 10.5 p272 // incremental sequence dlta [0 .. t-1] orders sequential table L by hill. Void shellsort (sqlist * l, int dlta [], int t) {int K; For (k = 0; k <t; ++ K) {// One increment is the insert sorting of dlta [k] shellinsert (L, dlta [k]); printf ("% d sort sorting result :", k + 1); print (* l) ;}# define N 8 # define t 3int main () {redtype d [N] ={{ 49, 1 }, {38, 2}, {65, 3}, {97, 4}, {76, 5}, {13, 6}, {27, 7}, {49, 8 }}; sqlist L; int I; int dt [T] = {5, 3, 1}; // incremental sequence array // to L. r assignment for (I = 0; I <n; I ++) L. R [I + 1] = d [I]; L. length = N; printf ("Before sorting: \ n"); print (L ); /***********************************/# If 0 printf ("\ n direct insert sorting process \ n "); insertsort (& L); printf ("\ n directly inserted after sorting: \ n"); print (L ); # endif/************* test the semi-insertion sorting *******************/ # If 0 printf ("\ n semi-insertion sorting process \ n "); binsertsort (& L); printf ("\ n semi-inserted after sorting: \ n"); print (L ); # endif/************* test the 2-way insertion sorting ******************* /# If 0p2_insertsort (& L ); printf ("\ N2 insert sorted: \ n"); print (L ); # endif *******************/# if 0 shellsort (& L, DT, T); printf ("\ n after Hill sorting: \ n"); print (l); # endifreturn 0;}/* output effect: * ********************************** before sorting: (49, 1) (38, 2) (65, 3) (97, 4) (76, 5) (13, 6) (27, 7) (49, 8) directly Insert the sorting process (38, 2) (49, 1) (65, 3) (97, 4) (76, 5) (13, 6) (27, 7) (49, 8) (38, 2) (49, 1) (65, 3) (76, 5) (97, 4) (13, 6) (27, 7) (49, 8) (13, 6) (38, 2) (49, 1) (65, 3) (76, 5) (97, 4) (27, 7) (49, 8) (13, 6) (27, 7) (38, 2) (49, 1) (65, 3) (76, 5) (97, 4) (49, 8) (13, 6) (27, 7) (38, 2) (49, 1) (49, 8) (65, 3) (76, 5) (97, 4) Insert directly after sorting: (13, 6) (27, 7) (38, 2) (49, 1) (49, 8) (65, 3) (76, 5) (97, 4) press any key to continue... * ********************************** before sorting: (49, 1) (38, 2) (65, 3) (97, 4) (76, 5) (13, 6) (27, 7) (49, 8) semi-insert sorting process (38, 2) (49, 1) (65, 3) (97, 4) (76, 5) (13, 6) (27, 7) (49, 8) (38, 2) (49, 1) (65, 3) (97, 4) (76, 5) (13, 6) (27, 7) (49, 8) (38, 2) (49, 1) (65, 3) (97, 4) (76, 5) (13, 6) (27, 7) (49, 8) (38, 2) (49, 1) (65, 3) (76, 5) (97, 4) (13, 6) (27, 7) (49, 8) (13, 6) (38, 2) (49, 1) (65, 3) (76, 5) (97, 4) (27, 7) (49, 8) (13, 6) (27, 7) (38, 2) (49, 1) (65, 3) (76, 5) (97, 4) (49, 8) (13, 6) (27, 7) (38, 2) (49, 1) (49, 8) (65, 3) (76, 5) (97, 4) after semi-insert sorting: (13, 6) (27, 7) (38, 2) (49, 1) (49, 8) (65, 3) (76, 5) (97, 4) press any key to continue... * Before sorting :( 49, 1) (38, 2) (65, 3) (97, 4) (76, 5) (13, 6) (27, 7) (49, 8) after 2 inserts are sorted: (13, 6) (27, 7) (38, 2) (49, 1) (49, 8) (65, 3) (76, 5) (97, 4) press any key to continue... * ********************************** before sorting: (49, 1) (38, 2) (65, 3) (97, 4) (76, 5) (13, 6) (27, 7) (49, 8) sorting result of 1st bits: (13, 6) (27, 7) (49, 8) (97, 4) (76, 5) (49, 1) (38, 2) (65, 3) 2nd sort results: (13, 6) (27, 7) (49, 8) (38, 2) (65, 3) (49, 1) (97, 4) (76, 5) 3rd sort results: (13, 6) (27, 7) (38, 2) (49, 8) (49, 1) (65, 3) (76, 5) (97, 4) after Hill sorting: (13, 6) (27, 7) (38, 2) (49, 8) (49, 1) (65, 3) (76, 5) (97, 4) press any key to continue... */

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.