2-路插入排序(2-way Insertion Sort)的C语言实现
2016-01-03 21:16
543 查看
原创文章,转载请注明来自钢铁侠Mac博客http://www.cnblogs.com/gangtiexia
2-路插入排序(2-way Insertion Sort)的基本思想:
比fisrt小的元素,插入first前面;
比final大的元素,插入final后面,
比fisrt大且比final小的元素插中间
演示实例:
C语言实现(编译器Dev-c++5.4.0,源代码后缀.cpp)
2-路插入排序(2-way Insertion Sort)的基本思想:
比fisrt小的元素,插入first前面;
比final大的元素,插入final后面,
比fisrt大且比final小的元素插中间
演示实例:
C语言实现(编译器Dev-c++5.4.0,源代码后缀.cpp)
#include <stdio.h> #define LEN 6 typedef float keyType; typedef struct{ keyType score; char name[20]; }student; typedef struct{ int length=LEN; student stu[LEN]; }sqList; void two_WayIS(sqList &L){ sqList temp; int first=1,final=1; temp.stu[first]=L.stu[1]; for(int i=2;i<L.length;i++){ if(L.stu[i].score>temp.stu[final].score){//插final后面 temp.stu[i]=L.stu[i]; final++; }else if(L.stu[i].score<temp.stu[first].score){ //插first前面 if(first==1) first--; //数组以1开始 first=(first-1+L.length)%L.length; //first变化为1->5->4>3>2...... temp.stu[first]=L.stu[i]; }else if(L.stu[i].score<temp.stu[final].score){ //插中间 int p=(final-1+L.length)%L.length; if(p==1) p--; while(L.stu[i].score<temp.stu[p].score){ p=(p-1+L.length)%L.length; if(p==1) p--; } final++; int k=final; while(k>(p+1)){ temp.stu[k]=temp.stu[k-1]; k=(k-1+L.length)%L.length; if(k==1) k--; } temp.stu[p+1]=L.stu[i]; } } } int main(){ sqList L; for(int i=1;i<L.length;i++){ printf("\n请输入第%d个学生的姓名:",i); gets(L.stu[i].name); printf("分数:"); scanf("%f",&(L.stu[i].score)); getchar(); } two_WayIS(L); for(int i=1;i<L.length;i++){ printf("\n学生%s 分数%f 第%d名",L.stu[i].name,L.stu[i].score,i); } return 1; }
相关文章推荐
- 折半插入排序(Binary Insertion Sort)的C语言实现
- 直接插入排序(Straight Insertion Sort)的C语言实现
- C语言内存管理:malloc、calloc、free的实现
- 总结文件操作函数-文件夹(三)-C语言
- 关于binary diff / patch 差分补丁工具的那些事
- C语言-函数指针
- 算法导论学习---红黑树具体解释之插入(C语言实现)
- 验证尼科彻斯定理
- ImmDisableIME在xp下的bug
- C++ 知识点
- C++里面的inl文件
- C++ 中 stack和queue使用方法
- C语言游戏
- c++内存泄漏处理(积累)
- C++ Primer Plus (第六版) 编程练习3.7
- C语言-动态内存分配
- C++程序设计课后习题及解答(第七章)
- C++程序设计课后习题及解答(第八章)
- c++20道面试题
- C++程序设计课后习题及解答(第四章)