排序算法课程设计专业班级学号姓名指导老师一:课程设计的目的1.掌握各种排序的基本思想2.掌握各种排序的算法实现3.掌握各种排序的算法优劣分析花费的时间计算4.掌握各种排序算法所适用的不同场合。
二:课程设计的内容(1)冒泡、直插、选择、快速、希尔、归并、堆排序算法进行比较;(2)待排序的元素的关键字为整数。
其中的数据用伪随机产生程序产生(如10000个,1000个),再使用各种算法对其进行排序,记录其排序时间,再汇总比较;(3)将每次测试所用的时间,用条形图进行表示,以便比较各种排序的优劣。
三:课程设计的实现(1)直接插入排序#include <iostream.h>typedef int keytype;struct datatype{keytype key;};/* int rand(void);void srand(unsigned int seed );*/#include<stdlib.h>#include<stdio.h>#include<time.h>#include<windows.h>void InsertSort (datatype a[], int n)//用直接插入法对a[0]--a[n-1]排序{int i, j;datatype temp;for(i=0; i<n-1; i++){temp = a[i+1];j = i;while(j > -1 && temp.key <= a[j].key){a[j+1] = a[j];j--;}a[j+1] = temp;}}void main(){/*srand((unsigned)time(NULL));// 随机种子*//*time_t t;srand((unsigned)time(&t));*/time_t t1,t2;srand((unsigned)GetCurrentTime());datatype num[10000];t1=GetCurrentTime();for(int i=0;i<10000;i++){num[i].key=rand();}int n=10000;InsertSort(num,n);for(int j=0;j<10000;j++)cout<<num[j].key<<endl;t2=GetCurrentTime();cout<<"The t1 time is:"<<t1<<"The t2 time is:"<<t2<<" t2-t1:"<<t2-t1<<endl;getchar();}(2)希尔排序#include <iostream.h>/* int rand(void);void srand(unsigned int seed );*/#include<stdlib.h>#include<stdio.h>#include<time.h>#include<windows.h>typedef int keytype;struct datatype{keytype key;};void ShellSort (datatype a[], int n, int d[], int numOfD)//用希尔排序法对记录a[0]--a[n-1]排序//各组内采用直接插入法排序{int i, j, k, m, span;datatype temp;for(m = 0; m < numOfD; m++){span = d[m];for(k = 0; k < span; k++){for(i = k; i < n-span; i = i+span){temp = a[i+span];j = i;while(j > -1 && temp.key <= a[j].key){a[j+span] = a[j];j = j-span;}a[j+span] = temp;}}}}void main(){/*srand((unsigned)time(NULL));// 随机种子*/ /*time_t t;srand((unsigned)time(&t));*/time_t t1,t2;srand((unsigned)GetCurrentTime());datatype num[10000];t1=GetCurrentTime();for(int i=0;i<10000;i++){num[i].key=rand();}int n=10000, d[]={1000,100,10,1},numOfd=4;ShellSort (num,n,d,numOfd);for(int j=0;j<10000;j++)cout<<num[j].key<<endl;t2=GetCurrentTime();cout<<"The t1 time is:"<<t1<<"The t2 time is:"<<t2<<" t2-t1:"<<t2-t1<<endl;getchar();}(3)直接选择排序#include <iostream.h>typedef int keytype;struct datatype{keytype key;};/* int rand(void);void srand(unsigned int seed );*/#include<stdlib.h>#include<stdio.h>#include<time.h>#include<windows.h>void SelectSort(datatype a[], int n)/*用直接选择排序法对a[0]--a[n-1]排序*/{int i, j, s;datatype temp;for(i=0; i < n-1; i++){s = i;for(j = i+1; j < n; j++)if(a[j].key < a[s].key) s=j;if(s != i){temp = a[i];a[i] = a[s];a[s] = temp;}}}void main(){/*srand((unsigned)time(NULL));// 随机种子*//*time_t t;srand((unsigned)time(&t));*/time_t t1,t2;srand((unsigned)GetCurrentTime());datatype num[10000];t1=GetCurrentTime();for(int i=0;i<10000;i++){num[i].key=rand();}int n=10000;SelectSort(num,n);for(int j=0;j<10000;j++)cout<<num[j].key<<endl;t2=GetCurrentTime();cout<<"The t1 time is:"<<t1<<"The t2 time is:"<<t2<<" t2-t1:"<<t2-t1<<endl;getchar();}(4)堆排序#include <iostream.h>typedef int keytype;struct datatype{keytype key;};#include<stdlib.h>#include<stdio.h>#include<time.h>#include<windows.h>void CreatHeap (datatype a[], int n, int h){int i, j, flag;datatype temp;i = h; // i为要建堆的二叉树根结点下标j = 2*i+1; // j为i的左孩子结点的下标temp = a[i];flag = 0;//沿左右孩子中值较大者重复向下筛选while(j < n && flag != 1){//寻找左右孩子结点中的较大者,j为其下标if(j < n-1 && a[j].key < a[j+1].key) j++;if(temp.key > a[j].key) //a[i].key>a[j].keyflag=1; //标记结束筛选条件else //否则把a[j]上移{a[i] = a[j];i = j;j = 2*i+1;}}a[i] = temp; //把最初的a[i]赋予最后的a[j]}void 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); //初始化创建最大堆for(i = n-1; i > 0; i--) //当前最大堆个数每次递减1{//把堆顶a[0]元素和当前最大堆的最后一个元素交换temp = a[0];a[0] = a[i];a[i] = 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 num[10000];t1=GetCurrentTime();for(int i=0;i<10000;i++){num[i].key=rand();}int n=10000;HeapSort(num, n);for(int j=0;j<10000;j++)cout<<num[j].key<<endl;t2=GetCurrentTime();cout<<"The t1 time is:"<<t1<<"The t2 time is:"<<t2<<" t2-t1:"<<t2-t1<<endl;getchar();}(5)冒泡排序#include <iostream.h>typedef int keytype;struct datatype{keytype key;};#include<stdlib.h>#include<stdio.h>#include<time.h>#include<windows.h>void BubbleSort(datatype a[], int n)//用冒泡排序法对a[0]--a[n-1]排序{int i, j, flag=1;datatype temp;for(i = 1; i < n && flag == 1; i++){flag = 0;for(j = 0; j < n-i; j++){if(a[j].key > a[j+1].key){flag = 1;temp = a[j];a[j] = a[j+1];a[j+1] = temp;}}}}void main(){/*srand((unsigned)time(NULL));// 随机种子*//*time_t t;srand((unsigned)time(&t));*/time_t t1,t2;srand((unsigned)GetCurrentTime());datatype num[10000];t1=GetCurrentTime();for(int i=0;i<10000;i++){num[i].key=rand();}int n=10000;BubbleSort(num, n);for(int j=0;j<10000;j++)cout<<num[j].key<<endl;t2=GetCurrentTime();cout<<"The t1 time is:"<<t1<<"The t2 time is:"<<t2<<" t2-t1:"<<t2-t1<<endl;getchar();}(6)快速排序#include <iostream.h>/* int rand(void);void srand(unsigned int seed );*/#include<stdlib.h>#include<stdio.h>#include<time.h>#include<windows.h>typedef int keytype;struct datatype{keytype key;};void QuickSort(datatype a[], int low, int high)//用递归方法对对象a[low]--a[high]进行快速排序{int i, j;datatype temp;i = low;j = high;temp = a[low];while(i < j){//在数组的右端扫描while(i < j && temp.key <= a[j].key) j--;if(i < j){a[i] = a[j];i++;}//在数组的左端扫描while(i < j && a[i].key < temp.key) i++;if(i < j){a[j] = a[i];j--;}}a[i] = temp;//对子对象数组进行递归快速排序if(low < i) QuickSort(a, low, i-1);if(i < high) QuickSort(a, j+1, high);}void main(){/*srand((unsigned)time(NULL));// 随机种子*//*time_t t;srand((unsigned)time(&t));*/time_t t1,t2;srand((unsigned)GetCurrentTime());datatype num[10000];t1=GetCurrentTime();for(int i=0;i<10000;i++){num[i].key=rand();}int n=10000;QuickSort(num, 0, 9999);for(int j=0;j<10000;j++)cout<<num[j].key<<endl;t2=GetCurrentTime();cout<<"The t1 time is:"<<t1<<"The t2 time is:"<<t2<<" t2-t1:"<<t2-t1<<endl;getchar();}(7)归并排序#include <iostream.h>/* int rand(void);void srand(unsigned int seed );*/#include<iostream.h>#include<stdlib.h>#include<stdio.h>#include<time.h>#include<windows.h>typedef int keytype;struct datatype{keytype key;};void Merge(datatype a[], int n, datatype swap[], int k)//对有序表a[0]--a[n-1]进行一次二路归并排序,每个有序子表的长度为k//一次二路归并排序后新的有序子表存于swap中{int m = 0, u1,l2,i,j,u2;int l1 = 0; //给出第一个有序子表下界while(l1+k <= n-1){l2 = l1 + k; //计算第二个有序子表下界u1 = l2 - 1; //计算第一个有序子表上界u2 = (l2+k-1 <= n-1)? l2+k-1: n-1; //计算第二个有序子表上界//两个有序子表合并for(i = l1, j = l2; i <= u1 && j <= u2; m++){if(a[i].key <= a[j].key){swap[m] = a[i];i++;}else{swap[m]=a[j];j++;}}//子表2已归并完,将子表1中剩余的对象顺序存放到数组swap中while(i <= u1){swap[m] = a[i];m++;i++;}//子表1已归并完,将子表2中剩余的对象顺序存放到数组swap中while(j <= u2){swap[m] = a[j];m++;j++;}l1 = u2 + 1;}//将原始数组中不足二组的对象顺序存放到数组swap中for(i = l1; i < n; i++, m++) swap[m] = a[i];}void MergeSort(datatype a[], int n)//用二路归并排序法对对象数组a[0]--a[n-1]排序,swap用于临时存放对象{int i, k = 1; //归并长度由1开始datatype *swap = new datatype[n]; //动态数组申请while(k < n){Merge(a, n, swap, k);//将记录从数组swap放回a中for(i = 0; i < n; i++) a[i] = swap[i];//归并长度加倍k = 2 * k;}delete []swap;}void main(){/*srand((unsigned)time(NULL));// 随机种子*//*time_t t;srand((unsigned)time(&t));*/time_t t1,t2;srand((unsigned)GetCurrentTime());datatype num[10000];t1=GetCurrentTime();for(int i=0;i<10000;i++){num[i].key=rand();}int n=10000;MergeSort(num, n);for(int j=0;j<10000;j++)cout<<num[j].key<<endl;t2=GetCurrentTime();cout<<"The t1 time is:"<<t1<<"The t2 time is:"<<t2<<" t2-t1:"<<t2-t1<<endl;getchar();}四:课程设计的结果对10000个随机数排序所用时间对1000个随机数排序所用时间用图的形式表示如下:直接插入希尔排序直接选择堆排序冒泡排序快速排序归并排序五:课程设计的结论在数据多的情况下,归并排序能够在最短的运行中完成排序,其次希尔排序,堆排序,快速排序。