项目 1 - 验证算法之(2)希尔排序
来源:互联网 发布:mysql 按某字段排序 编辑:程序博客网 时间:2024/05/30 12:03
/*
*All right reserved.
*文件名称:test.cpp
*作者:杨天瑞
*完成日期:2016年12月17日
*版本号:v1.7.9
*
* 问题描述:用序列{57, 40, 38, 11, 13, 34, 48, 75, 6, 19, 9, 7}作为测试数据,运行并视频中算法对应 程序,观察运行结果并深刻领会算法的思路和实现方法。
* 程序输入:无。
* 程序输出:排序结果。
*/
1.希尔排序
xier.cpp:
#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void ShellSort(RecType R[],int n) //希尔排序算法{ int i,j,gap; RecType tmp; gap=n/2; //增量置初值 while (gap>0) { for (i=gap; i<n; i++) //对所有相隔gap位置的所有元素组进行排序 { tmp=R[i]; j=i-gap; while (j>=0 && tmp.key<R[j].key)//对相隔gap位置的元素组进行排序 { R[j+gap]=R[j]; j=j-gap; } R[j+gap]=tmp; j=j-gap; } gap=gap/2; //减小增量 }}int main(){ int i,n=11; RecType R[MaxSize]; KeyType a[]= {16,25,12,30,47,11,23,36,9,18,31}; for (i=0; i<n; i++) R[i].key=a[i]; printf("排序前:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); ShellSort(R,n); printf("排序后:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); return 0;}
2.排序中输出每一趟的中间结果:
xier1.cpp:
#include <stdio.h>#define MaxSize 20typedef int KeyType; //定义关键字类型typedef char InfoType[10];typedef struct //记录类型{ KeyType key; //关键字项 InfoType data; //其他数据项,类型为InfoType} RecType; //排序的记录类型定义void InsertSort(RecType R[],int n) //对R[0..n-1]按递增有序进行直接插入排序{ int i,j,k; RecType tmp; for (i=1; i<n; i++) { tmp=R[i]; j=i-1; //从右向左在有序区R[0..i-1]中找R[i]的插入位置 while (j>=0 && tmp.key<R[j].key) { R[j+1]=R[j]; //将关键字大于R[i].key的记录后移 j--; } R[j+1]=tmp; //在j+1处插入R[i] printf("i=%d: ",i); for (k=0; k<n; k++) printf("%d ",R[k].key); printf("\n"); }}int main(){ int i,n=10; RecType R[MaxSize]; KeyType a[]= {9,8,7,6,5,4,3,2,1,0}; for (i=0; i<n; i++) R[i].key=a[i]; printf("排序前:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); InsertSort(R,n); printf("排序后:"); for (i=0; i<n; i++) printf("%d ",R[i].key); printf("\n"); return 0;}
0 0
- 项目 1 - 验证算法之(2)希尔排序
- 第十五周项目1 验证算法(2)插入排序之希尔排序
- 第十六周项目1-验证算法(2)希尔排序
- 第十六周项目1-验证算法(2)希尔排序
- 第十六周项目1-验证算法(2)希尔排序
- 第十六周项目1-验证算法(2)希尔排序
- 【第十五周项目1 - 验证算法之希尔排序】
- 【第十五周项目1 - 验证算法之希尔排序】
- 第14周项目1(2)- 验证算法 希尔排序
- 第十六周 项目一 验证算法(2) 希尔排序
- 第十五周项目1--验证算法--(3)希尔排序
- 第十五周 项目1--验证算法--(3)希尔排序
- 第十五周项目1-验证算法(3-希尔排序)
- 第十五周项目1-验证算法(3)-希尔排序
- 第十五周项目1(3)--验证希尔排序算法
- 第十五周项目1-(3)-验证希尔排序算法
- 第十五周项目1(3)--验证希尔排序算法
- 第十五周项目1-(3)-验证希尔排序算法
- STL heap 实现
- OpenCV 3学习笔记(四)读取并播放视频
- 【24.17%】【codeforces 721D】Maxim and Array
- 2D平面变换总结
- React Native之ScrollView控件详解
- 项目 1 - 验证算法之(2)希尔排序
- 我的简书
- Microsoft Mathematics (64 位)
- 关于window.location.href 传中文参数 乱码问题
- 在CentOS 7/6.5/6.4 中安装Java JDK 8
- 使用Kotlin开发Android遇到的问题
- WEB前端之HTML5[4]~HTML5改良的input元素的种类
- hdp ambari Confirm Hosts 部署错误问题
- eclipse调试web项目错误