《插入排序算法实现》由会员分享,可在线阅读,更多相关《插入排序算法实现(7页珍藏版)》请在金锄头文库上搜索。
1、插入排序算法实现插入排序算法实现#include#include#include#include/#define MAX 10000#define END 20000/struct node int val;struct node *pre;struct node *next;/struct node *Bnode(struct node *p,int n);struct node *Insertsort(struct node *p,int flag);/int main(void)int i,sum,num,flag,mode;struct node *List = NULL;printf
2、(“Please choose the serial number of sort mode:n“);printf(“t1.input by yourself.n“);printf(“t2.input by the computer itself.n“);scanf(“%d“,if(mode = 2)printf(“Please input the total number needed to sortn“);scanf(“%d“,printf(“Please input the order of sort by number:n“);printf(“t1.升序n“);printf(“t2.降
3、序n“);scanf(“%d“,srand(unsigned)time(NULL);for(i = 0; i val = n;temp-pre = NULL;temp-next = NULL;p = temp;elseq = p;while(q-next != NULL)q = q-next;temp = (struct node *)malloc(sizeof(struct node);temp-val = n;temp-pre = q;temp-next = NULL;q-next = temp;return p;/struct node *Insertsort(struct node *
4、p,int flag)struct node *temp,*next;struct node *q,*q1;FILE *fp;int i = 0;flag = flag -1;fp = fopen(“F:Insertsort.csv“,“w“);temp = p;fprintf(fp,“初始数据t,“);while(temp != NULL)fprintf(fp,“%-5d,“,temp-val);temp = temp-next;fprintf(fp,“n“);fprintf(fp,“n“);temp = p;fprintf(fp,“趟数列标,“);while(temp != NULL)fp
5、rintf(fp,“%-5d,“,+i);temp = temp-next;fprintf(fp,“n“);/fprintf(fp,“-n“);i = 0;for(q = p-next; q != NULL; q = next)i+;temp = q1 = q;next = q-next;if(q1 -pre-val q1-val if(q1-next != NULL)q1-next-pre = q1-pre;q1 =q1-pre;while(q1-val temp-val if(q1 = NULL)temp-next = p;p-pre = temp;temp-pre = NULL;p =
6、temp;break;if(q1 != NULL)temp-next = q1-next;q1-next-pre = temp;q1-next = temp;temp-pre = q1;temp = p;fprintf(fp,“第%d 趟t,“,i);while(temp != NULL)fprintf(fp,“%-5d,“,temp-val);temp = temp-next;fprintf(fp,“n“);/*endfor*/fprintf(fp,“*n“);fprintf(fp,“n“);temp = p;fprintf(fp,“排序结果t,“);while(temp != NULL)fprintf(fp,“%-5d,“,temp-val);temp = temp-next;if(!flag)fprintf(fp,“(升序),“);elsefprintf(fp,“(降序),“);fprintf(fp,“n“);fclose(fp);system(“F:Insertsort.csv“);system(“del F:Insertsort.csv“);return p;