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