排序算法课程设计(共14页).doc
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《排序算法课程设计(共14页).doc》由会员分享,可在线阅读,更多相关《排序算法课程设计(共14页).doc(14页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、精选优质文档-倾情为你奉上 排序算法课程设计专 业 班 级 学 号 姓 名 指导老师 一:课程设计的目的1. 掌握各种排序的基本思想2. 掌握各种排序的算法实现3. 掌握各种排序的算法优劣分析花费的时间计算4. 掌握各种排序算法所适用的不同场合。二:课程设计的内容(1)冒泡、直插、选择、快速、希尔、归并、堆排序算法进行比较;(2)待排序的元素的关键字为整数。其中的数据用伪随机产生程序产生(如10000个,1000个),再使用各种算法对其进行排序,记录其排序时间,再汇总比较;(3)将每次测试所用的时间,用条形图进行表示,以便比较各种排序的优劣。三:课程设计的实现(1) 直接插入排序#includ
2、e typedef int keytype;struct datatypekeytype key;/* int rand(void); void srand(unsigned int seed ); */#include#include#include#includevoid InsertSort (datatype a, int n)/用直接插入法对a0-an-1排序int i, j;datatype temp;for(i=0; i -1 & temp.key = aj.key)aj+1 = aj;j-;aj+1 = temp;void main() /*srand(unsigned)tim
3、e(NULL);/ 随机种子 */*time_t t;srand(unsigned)time(&t);*/time_t t1,t2;srand(unsigned)GetCurrentTime();datatype num10000;t1=GetCurrentTime();for(int i=0;i10000;i+)numi.key=rand(); int n=10000;InsertSort(num,n);for(int j=0;j10000;j+) coutnumj.keyendl;t2=GetCurrentTime();coutThe t1 time is:t1The t2 time is
4、:t2 t2-t1:t2-t1endl;getchar(); (2)希尔排序#include /* int rand(void); void srand(unsigned int seed ); */#include#include#include#includetypedef int keytype;struct datatypekeytype key;void ShellSort (datatype a, int n, int d, int numOfD)/用希尔排序法对记录a0-an-1排序/各组内采用直接插入法排序int i, j, k, m, span;datatype temp;f
5、or(m = 0; m numOfD; m+)span = dm;for(k = 0; k span; k+)for(i = k; i -1 & temp.key = aj.key)aj+span = aj;j = j-span;aj+span = temp;void main() /*srand(unsigned)time(NULL);/ 随机种子 */*time_t t;srand(unsigned)time(&t);*/time_t t1,t2;srand(unsigned)GetCurrentTime();datatype num10000;t1=GetCurrentTime();fo
6、r(int i=0;i10000;i+)numi.key=rand(); int n=10000, d=1000,100,10,1,numOfd=4;ShellSort (num,n,d,numOfd); for(int j=0;j10000;j+) coutnumj.keyendl;t2=GetCurrentTime();coutThe t1 time is:t1The t2 time is:t2 t2-t1:t2-t1endl;getchar(); (3)直接选择排序#include typedef int keytype;struct datatypekeytype key;/* int
7、 rand(void); void srand(unsigned int seed ); */#include#include#include#includevoid SelectSort(datatype a, int n)/*用直接选择排序法对a0-an-1排序*/int i, j, s;datatype temp;for(i=0; i n-1; i+)s = i;for(j = i+1; j n; j+)if(aj.key as.key) s=j;if(s != i)temp = ai;ai = as;as = temp;void main() /*srand(unsigned)time
8、(NULL);/ 随机种子 */*time_t t;srand(unsigned)time(&t);*/time_t t1,t2;srand(unsigned)GetCurrentTime();datatype num10000;t1=GetCurrentTime();for(int i=0;i10000;i+)numi.key=rand(); int n=10000;SelectSort(num,n); for(int j=0;j10000;j+) coutnumj.keyendl;t2=GetCurrentTime();coutThe t1 time is:t1The t2 time is
9、:t2 t2-t1:t2-t1endl;getchar(); (4)堆排序#include typedef int keytype;struct datatypekeytype key;#include#include#include#includevoid CreatHeap (datatype a, int n, int h)int i, j, flag;datatype temp;i = h; / i为要建堆的二叉树根结点下标j = 2*i+1;/ j为i的左孩子结点的下标temp = ai;flag = 0;/沿左右孩子中值较大者重复向下筛选while(j n & flag != 1)
10、/寻找左右孩子结点中的较大者,j为其下标if(j n-1 & aj.key aj.key)/ai.keyaj.keyflag=1;/标记结束筛选条件else/否则把aj上移ai = aj;i = j;j = 2*i+1;ai = temp;/把最初的ai赋予最后的ajvoid InitCreatHeap(datatype a, int n)int i;for(i = (n-1)/2; i = 0; i-)CreatHeap(a, n, i);void HeapSort(datatype a, int n)int i;datatype temp; InitCreatHeap(a, n);/初始化
11、创建最大堆for(i = n-1; i 0; i-)/当前最大堆个数每次递减1/把堆顶a0元素和当前最大堆的最后一个元素交换temp = a0;a0 = ai;ai = temp;CreatHeap(a, i, 0);/调整根结点满足最大堆void main() /*srand(unsigned)time(NULL);/ 随机种子 */*time_t t;srand(unsigned)time(&t);*/time_t t1,t2;srand(unsigned)GetCurrentTime();datatype num10000;t1=GetCurrentTime();for(int i=0;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 排序 算法 课程设计 14
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内