Inner sort, insert sort, other insert sort, table insert sort

Source: Internet
Author: User

Text description

And before the insertion sort ratio, the table insert sort can guarantee the sorting process does not move the record, so the table insert sort uses the storage structure and the previous order store different, the table insert sort takes the static list type as the storage structure of the sequence of records to be sorted, sets the component of subscript 0 in the array to the table header node. And makes the key of the table header node record the largest integer maxint. The basic operation of table insertion sorting is still to insert a record into an ordered table that is already sorted, with a similarity to the direct insertion sort, except that it replaces 1 movement records with a 2-time pointer change.

Algorithm analysis

The time complexity is still n*n, because the ordering process requires the same number of pointer values as the data, so the secondary space is N and is a stable sorting method.

Code implementation

1#include <stdio.h>2#include <stdlib.h>3 4 //static linked list capacity5 #defineSIZE 1006 //Maximum integer7 #defineMAX 1000008 9 #defineEQ (A, B) ((a) = = (b))Ten #defineLT (A, B) ((a) < (b)) One #defineLQ (A, B) ((a) <= (b)) A  - //defining a keyword type as an integer type -typedefintKeyType; the //define other data items as integer types -typedefintInfoType; -  - //Record Type +typedefstruct{ -     //keyword Items + KeyType key; A     //Additional Data Items at InfoType Otherinfo; - }redtype;  -  - //Table Node types -typedefstruct{ -     //Record Entry in Redtype RC; -     //pointer Item to     intNext; + }slnode; -  the //static linked list type *typedefstruct{ $     //Unit No. 0 is the table header node.Panax Notoginseng Slnode r[size]; -     //Linked list current length the     intlength; + }slinklisttype; A  the //Sequential printing of keywords in a static linked list and pointers to the next data + voidprintslist (slinklisttype SL) - { $     inti =0; $printf"len:%d; data:\n\n", sl.length); -      for(i=0; i<=sl.length; i++){ -printf"\t[%d]key=%d,\tnext=%d\n", I, Sl.r[i].rc.key, sl.r[i].next); the     } -printf"\ n");Wuyi     return ; the } -  Wu //table insertion Sorting algorithm - voidTableinsertsort (Slinklisttype *SL) About { $     //No. 0-bit header node with maximum integer value Max -sl->r[0].rc.key =MAX; -     //first, the component labeled 1 in the static linked list and the header node form a circular linked list . -sl->r[0].next =1; Asl->r[1].next =0; +  the     //similar to the direct insert sort, just don't move the record, just change the pointer to refer to -     inti =0, q =0, p =0; $      for(i=2; i<=sl->length; i++){ theQ =0; thep = sl->R[q].next; the          while(LT (Sl->r[p].rc.key, sl->R[i].rc.key)) { theQ =p; -p = sl->R[q].next; in         } the         //to change the 2-time pointer value instead of the move record theSl->r[q].next =i; AboutSl->r[i].next =p; the     } the     return ; the } +  - intMainintargcChar*argv[]) the {Bayi     if(ARGC <2){ the         return-1; the     } - Slinklisttype SL; -     inti =0; the      for(i=1; i<argc; i++){ the         if(i>SIZE) the              Break; theSl.r[i].rc.key =atoi (Argv[i]); -Sl.r[i].next =0; the     } theSl.length = (I-1); theTableinsertsort (&SL);94 printslist (SL); the     return 0; the}
Table Insert Sort

Run

[Email protected] data.structure]$./a.out 4 5 7 34 5 23
Len:6; Data

[0]key=100000,next=1
[1]key=4,next=5
[2]key=5,next=3
[3]key=7,next=6
[4]key=34,next=0
[5]key=5,next=2
[6]key=23,next=4

[Email protected] data.structure]$

Inner sort, insert sort, other insert sort, table insert sort

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.