Straight Insert Sort
来源:互联网 发布:avast知乎 编辑:程序博客网 时间:2024/06/06 07:11
直接插入排序
#include <stdio.h>#include <time.h> #define NODE_NUM 10 //定义节点的数目 struct record {int key;};typedef struct {struct record r[NODE_NUM+1];int length;} SqList;typedef SqList HeapType; /* * Use the zero element as the sentinel * for example, direct sort sequence: * compare the data * 1. 8 | 5 4 7 1 (The initial data) * 1. 5 8 | 4 7 1 * 2. 4 5 8 | 7 1 * 3. 4 5 7 8 | 1 * 4. 1 4 5 7 8 | */void direct_insert_sort(HeapType *SR){int i, j;int temp;for (i = 2; i <= SR->length; i++) {SR->r[0].key = SR->r[i].key;temp = i;for (j = i - 1; j > 0; j--) {if (SR->r[j].key < SR->r[0].key) break;SR->r[j + 1].key = SR->r[j].key;temp = j;}SR->r[temp].key = SR->r[0].key;}}/* * Diminish the comparing data and moving data times * 由小到大排序 */void binary_insert_sort(HeapType *SR){int i, j;int mid;int low, high;for (i = 2; i <= SR->length; i++) {low = 1;high = i - 1;SR->r[0].key = SR->r[i].key;while (low <= high) {mid = (low + high) / 2;if (SR->r[mid].key <= SR->r[0].key) {low = mid + 1;} else {high = mid - 1;}}for (j = i - 1; j >= high + 1; j--) {SR->r[j + 1].key = SR->r[j].key;}SR->r[high + 1].key = SR->r[0].key;}}//执行主函数 int main(void){HeapType heaptree;HeapType newtree;int i; srand(time(0));heaptree.length = NODE_NUM; //顺序存储长度值 /*--------------------------------------*/for (i = NODE_NUM; i > 0; i--) {//初始化输入顺序序列,随机函数生成 heaptree.r[i].key = rand()%100+1;;}printf("The first Method: Heap sort before:\r\n");for (i = 1; i <= NODE_NUM; i++) {printf("Heap record[%02d]=%d\r\n", i, heaptree.r[i].key);}//direct_insert_sort(&heaptree);binary_insert_sort(&heaptree); //quick_sort(&heaptree, 1, heaptree.length);printf("The second Heap sort after:\r\n");for (i = 1; i <= NODE_NUM; i++) {printf("Heap record[%02d]=%d\r\n", i, heaptree.r[i].key);}return 0;}
2016.10.25 wiwa
参考数据结构 严蔚敏
0 0
- Straight Insert Sort
- 直接插入排序(Straight Insert Sort)
- Sorting Algorithm-Straight Insertion Sort
- insert sort
- Insert sort
- INSERT-SORT
- Insert Sort
- Insert Sort
- insert sort
- insert sort
- insert sort
- insert sort
- insert sort
- Insert Sort
- Insert sort
- 模板——Straight Insertion Sort
- 直接插入排序(Straight Insertion Sort)
- 直接插入排序(Straight Insertion Sort)
- ARM9嵌入式系统设计(基于S3C2410与Linux)——第二篇ARM体系结构
- 全栈工程师之路(一)—— CSS(网页样式语言)
- 服务接口统一返回的格式GOOD
- KVM添加文件夹存储
- memcached client -- spymemcached使用
- Straight Insert Sort
- C#封装MongoDb.Driver 实现EF操作SqlServer模式
- GROUP BY 与ORDER BY 与 limit 综合用法
- Shell Sort
- 【工作笔记】浏览器后退响应及关于input标签的一些说明
- 基于Swift3.0/OC的缓存清理功能的实现
- ubuntu添加vlan和路由追踪
- 背包问题动态规划算法java实现及分析
- 使用构造函数直接赋值与使用初始化列表的不同