每个标题下分别给出主函数和测试用例,总的排序代码放在最后,表排序有详细注释,方便理解。
目录
- int main(void)
- {
- int cnt ;
- SortObject *p ;
- p = (SortObject*)malloc(sizeof(SortObject));
- linkObject *head ;
- head = (linkObject*)malloc(sizeof(linkObject));
- head->next = NULL;
- scanf("%d",&cnt);
- p->NUM = cnt ;
- p->data = (DataType*)malloc(sizeof(DataType)*cnt);
- printf("the result of listSort:\n");
- for(int i=0;i
- {
- scanf("%d",&p->data[i]);
-
- }
- insertSort(p);
-
-
-
- }
测试输入:
8 49 38 65 97 76 113 27 49
—— 预期输出 ——
the result of listSort:
38 49 65 97 76 113 27 49
38 49 65 97 76 113 27 49
38 49 65 97 76 113 27 49
38 49 65 76 97 113 27 49
38 49 65 76 97 113 27 49
27 38 49 65 76 97 113 49
27 38 49 49 65 76 97 113
第2关二分插入排序
- int main(void)
- {
- int cnt ;
- SortObject *p ;
- p = (SortObject*)malloc(sizeof(SortObject));
- linkObject *head ;
- head = (linkObject*)malloc(sizeof(linkObject));
- head->next = NULL;
- scanf("%d",&cnt);
- p->NUM = cnt ;
- p->data = (DataType*)malloc(sizeof(DataType)*cnt);
- //printf("the result of binInsertSort:\n");
- for(int i=0;i
- {
- scanf("%d",&p->data[i]);
-
- }
- binInsertSort(p);
-
-
-
- }
测试输入:
8 38 49 65 97 76 113 27 49
—— 预期输出 ——
the result of binInsertSort:
38 49 65 97 76 113 27 49
38 49 65 97 76 113 27 49
38 49 65 97 76 113 27 49
38 49 65 76 97 113 27 49
38 49 65 76 97 113 27 49
27 38 49 65 76 97 113 49
27 38 49 49 65 76 97 113
第3关表插入排序
- int main(void)
- {
- int data ;
- int cnt ;
-
- linkObject *head,*p ;
- head = (linkObject*)malloc(sizeof(linkObject));
- head->next = NULL;
- scanf("%d",&cnt);
-
- for(int i=0;i
- {
- scanf("%d",&data);
- p = (linkObject*)malloc(sizeof(linkObject));
- p->next = head->next;
- p->info = data;
- head->next = p ;
- }
- printf("the result of linkSort:\n");
- listSort(head);
-
-
-
- }
第4关shell排序
- int main(void)
- {
- int cnt ;
- SortObject *p ;
- p = (SortObject*)malloc(sizeof(SortObject));
- linkObject *head ;
- head = (linkObject*)malloc(sizeof(linkObject));
- head->next = NULL;
- scanf("%d",&cnt);
- p->NUM = cnt ;
- p->data = (DataType*)malloc(sizeof(DataType)*cnt);
-
- for(int i=0;i
- {
- scanf("%d",&p->data[i]);
-
- }
- shellSort(p,cnt/2);
-
-
-
- }
测试输入:
8 49 38 65 97 13 76 27 49
—— 预期输出 ——
13 38 27 49 49 76 65 97
13 38 27 49 49 76 65 97
13 27 38 49 49 65 76 97
插入排序代码
- #include
- #include
-
- /*数据结构定义*/
- typedef int DataType;
- typedef struct
- {
- DataType *data; //用于存放待排序关键字的起始地址
- int NUM; //待排序关键字的个数
- } SortObject;
-
- typedef struct node //用于表插入排序的数据结构
- {
- DataType info;
- struct node *next;
- } linkObject;
-
- //输出顺序表
- void print(SortObject *p)
- {
- for(int i=0;i
NUM;i++) - printf("%d ",p->data[i]);
- printf("\n");
- }
-
- //输出链表
- void printLink(linkObject *Head)
- {
- linkObject *p = Head->next ;
- while(p)
- {
- printf("%d ",p->info);
- p = p->next;
- }
- printf("\n");
- }
-
- /*第一关
- 此处请填写代码实现递增序进行直接插入排序,
- 要求每趟排序后 调用print函数,输出关键字的排列情况*/
- void insertSort( SortObject *Rec )
- {
-
- /*----begin------*/
- DataType temp;
- DataType *data = Rec->data;
- int i,j;
-
- for(i=1;i
NUM;i++){ - temp = data[i];
- for(j = i-1 ; j >= 0 && temp
- data[j+1] = data[j];
- }
- data[j+1] = temp;
- print(Rec);
- }
-
- /*-----end------*/
-
- }
-
- /*第二关
- 此处请填写代码实现递增序进行二分插入排序,
-
- 实质是在已经有序的表中采用二分法查找插入位置
- 要求每趟排序后 调用print函数,输出关键字的排列情况*/
- void binInsertSort( SortObject *Rec )
- {
- printf("the result of binInsertSort:\n");
- /*----begin------*/
- int i,j,left,mid,right;
- DataType *data = Rec->data;
- for(i = 1;i
NUM;i++){ - DataType temp = data[i];
- left = 0,right = i-1;
- while(left<=right){
- mid = (left+right)>>1;
- if(temp>data[mid])left = mid+1;
- else right = mid - 1;
- }
- for(j = i-1;j>=left;j--)
- data[j+1] = data[j];
- data[left] = temp;
- print(Rec);
- }
- /*-----end------*/
-
- }
-
- /* 第四关
- 此处请填写代码实现递增序进行shell排序,
-
- 要求每趟排序后 调用print函数,输出关键字的排列情况
- */
- void shellSort( SortObject *Rec,int d )
- {
- int i,j,inc;
- DataType* temp;
- DataType* data = Rec->data;
- for(inc = d ; inc>0;inc/=2){
- for(i = inc;i
NUM;i++){ - temp = data[i];
- //这边都是inc范围的移动
- for(j = i - inc;j>=0&&data[j]>temp;j-=inc)
- data[j+inc] = data[j];
- data[j+inc] = temp;
- }
- print(Rec);
- }
- }
-
- /*第三关
- 此处请填写代码实现递增序进行表插入排序,
- 返回值是关键字比较次数
- Head是表头结点,不存放数据,info是待插入数据
- 要求每趟排序后 调用printLink函数,输出关键字的排列情况
- */
- void listSort(linkObject *plist )
- {
-
- /*----begin------*/
- linkObject *head,*p,*q,*pre,*now;
- head = plist;
- pre = head->next;
- if(pre==NULL)return;
- now = pre ->next;
- if(now==NULL)return;
- printLink(plist);
- while(now!=NULL){
- (void)(q = head),p = q->next;
- while(p!=now&&p->info
info){q = q->next,p = q->next;} - if(p==now){pre = pre->next;now = pre->next; continue;}
- pre->next = now->next;
- q->next = now;
- now->next = p;
- now = pre->next;
- printLink(plist);
- }
-
- /*-----end------*/
-
- }
-
相关阅读:
使用requests库解决Session对象设置超时的问题
C#医学检验室(LIS)信息管理系统源码
电脑软件:推荐一款非常实用的固态硬盘优化工具
ARM Linux DIY(十一)板子名称、开机 logo、LCD 控制台、console 免登录、命令提示符、文件系统大小
致远oa wpsassistservlet任意文件上传漏洞
java计算机毕业设计微服务在线考试源码+系统+数据库+lw文档
第4节-PhotoShop基础课程-Ps格式
为何vxlan需要封装在UDP里而不是直接使用IP包封装?
推荐系统(业务侧)小结
DPDK helloworld示例程序
-
原文地址:https://blog.csdn.net/qq_61735602/article/details/127714020
-
最新文章
-
沪漂五周年了:我越来越迷茫了
Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
MySQL-Seconds_behind_master的精度误差
[MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
Agent OS :五种驯服不确定性的范式
PortSwigger SQL注入LAB11
数据库即时编译JIT
[Begin]AI Learn Data Day 0
深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU