Internal sort, insert sort, direct insert sort

Source: Internet
Author: User

Text Description:

Inserts a record into an ordered, sorted table, resulting in a new, sequential table with a 1 increase in record count

:

Algorithm Analysis:

The time complexity is n*n, and the secondary storage is 1, which is a stable sorting method.

Code implementation:

1#include <stdio.h>2#include <stdlib.h>3 4 #defineEQ (A, B) ((a) = = (b))5 #defineLT (A, B) ((a) < (b))6 #defineLQ (A, B) ((a) <= (b))7 8 #defineMAXSIZE 209typedefintKeyType;TentypedefintInfoType; Onetypedefstruct{ A KeyType key; - InfoType Otherinfo; - }redtype; the  -typedefstruct{ -Redtype r[maxsize+1]; -     intlength; + }sqlist; -  + voidInsertsord (SqList *L) { A     inti =0, j =0; at      for(i=2; i<=l->length; i++){ -         if(LT (L->r[i].key, l->r[i-1].key)) { -l->r[0] = l->R[i]; -L->r[i] = l->r[i-1]; -              for(j=i-2; LT (l->r[0].key, L->r[j].key); --j) { -l->r[j+1] = l->R[j]; in             } -l->r[j+1] = l->r[0]; to         } +     } - } the  * voidprintlist (sqlist L) { $     inti =0;Panax Notoginsengprintf"len:%d; Data:", l.length); -      for(i=1; i<=l.length; i++){ theprintf"%d", L.r[i].key); +     } Aprintf"\ n"); the     return ; + } -  $  $ intMainintargcChar*argv[]) - { -     if(ARGC <2){ the         return-1; -     }Wuyi sqlist L; the     inti =0; -      for(i=1; i<argc; i++){ Wu         if(i>MAXSIZE) -              Break; AboutL.r[i].key =atoi (Argv[i]); $     } -L.length = (I-1); -  -Insertsord (&L); A printlist (L); +     return 0; the}
Direct Insert Sort

Run:

~$./a.out 3 5 7 3 1 9 6 4
Len:8; Data:1 3 3 4 5 6 7 9

Internal sort, insert sort, direct 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.