数据结构-折半插入排序
2015-03-24 20:22
127 查看
程序代码如下:
#include <stdio.h> #include <stdlib.h> #include <time.h> #define MAXSIZE 20 typedef int KeyType; typedef char InfoType; //结构体定义 typedef struct { KeyType key; InfoType otherinfo; }RedType; typedef struct { RedType r[MAXSIZE+1]; int length; }SqList; //各个函数定义 void print(SqList *L); void init(SqList *L); void BInsertSort(SqList *L); //初始化,随机产生待排序的数组 void init(SqList *L) { int n,i; printf("请输入待排序的元素个数:"); scanf("%d",&n); srand(((int)time(0)));//设置随机种子 for(i=1; i<=n; i++) { L->r[i].key = rand()%10+1; } L->length = n; printf("随机产生的待排数组为:"); print(L); printf("\n"); } //输出数组元素 void print(SqList *L) { int i; for(i=1; i<=L->length; i++) { printf("%d ",L->r[i].key); } printf("\n"); } //进行折半插入排序 void BInsertSort(SqList *L) { int low,mid,high,i,j; for(i=2; i<=L->length; i++) { L->r[0] = L->r[i]; low = 1; high = i-1; while(low<=high) { mid = (low+high)/2; if(L->r[0].key < L->r[mid].key) { high = mid-1; } else { low = mid+1; } } for(j=i-1; j>=high+1; --j) { L->r[j+1] = L->r[j]; } L->r[high+1] = L->r[0]; printf("第%d次排序的结果为:",i-1); print(L); } printf("\n"); } //主函数 int main() { SqList sq; init(&sq); BInsertSort(&sq); printf("排序之后的数组为:"); print(&sq); return 0; }
相关文章推荐
- 数据结构-插入排序-折半插入排序
- C++代码,数据结构-内部排序-插入排序-折半插入排序
- [算法与数据结构] - No.2 折半插入排序
- 数据结构-排序: 折半插入排序
- 一步步学习数据结构和算法之折半插入排序效率分析及java实现
- 数据结构之插入排序--折半插入排序
- 数据结构 - 折半插入排序(Binary Insertion Sort) 详解 及 代码(C++)
- 数据结构学习_折半插入排序
- 数据结构之插入排序--折半插入排序
- java数据结构之插入排序(直接插入排序、折半插入排序、希尔排序)
- 第十六周--数据结构--项目一--插入排序之折半插入排序
- 10-2-折半插入排序-内部排序-第10章-《数据结构》课本源码-严蔚敏吴伟民版
- 重温数据结构系列随笔:单链表(c#模拟实现)
- Nginx基本数据结构之ngx_str_t
- 数据结构JAVA--链表
- 第14周SHH数据结构-【项目1-(2)验证分块查找算法】
- C - 数据结构 - 线性表
- 数据结构_散列表
- 郝斌数据结构(1)----数据结构基本分类和链表创建