测量各排序算法的时间性能
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
实验坏境,VC++6.0
实验程序如下:
#include<iostream>
#include<stdio.h>
#include<time.h>
#include<windows.h>
using namespace std;
void BubbleSort(int r[], int n); //起泡排序
void SelectSort(int r[ ], int n); //简单选择排序
void QuickSort(int r[], int first, int end); //快速排序
void MergeSort1(int r[ ], int r1[ ], int n ); //归并排序的非递归算法
void MergeSort2(int r[], int r1[], int r2[],int s, int t);//归并排序的递归算法
const int size=100000;
int max=10000000,num[size];
int g1[size];
int h1[size];
int h2[size];
int main(){
//数组中的数据随机生成
srand((unsigned)time(NULL));
//cout<<"随机数组为:";
for(int i=0;i<size;i++)
{
num[i]=rand()%max;
// cout<<num[i]<<" ";
}
//cout<<endl;
//非递减有序
int
a[]={21,33,44,44,58,78,89,90,100,100,105,110,116,120,129,130,138,138,140,159,167,177,189,18 9,199,200,210,222,238,249,267,279,288,288,194,300,345,389,489,599,673,899,903,903,1003,127 7,1390,1390,1789,1990};
int b[1000],c[10000];
for(int j=0;j<1000;j++)
b[j]=j+10;
for(int k=0;k<10000;k++)
c[k]=k+5;
clock_t start,end;
cout<<"\n起泡排序结果为:";
/*start=clock();
BubbleSort(num,size);
end=clock();
cout<<"The time was:"<<(double)(end - start) / CLK_TCK<<endl;*/
start=clock();
BubbleSort(a,50);
end=clock();
cout<<"The time was:"<<(double)(end - start) / CLK_TCK<<endl;
start=clock();
BubbleSort(b,1000);
end=clock();
cout<<"The time was:"<<(double)(end - start) / CLK_TCK<<endl;
start=clock();
BubbleSort(c,10000);
end=clock();
cout<<"The time was:"<<(double)(end - start) / CLK_TCK<<endl;
cout<<"\n简单选择排序结果为:";
/*start=clock();
SelectSort(num,size);
end=clock();
cout<<"The time was:"<<(double)(end - start) / CLK_TCK<<endl;*/
start=clock();
SelectSort(a,50);
end=clock();
cout<<"The time was:"<<(double)(end - start) / CLK_TCK<<endl;
start=clock();
SelectSort(b,1000);
end=clock();
cout<<"The time was:"<<(double)(end - start) / CLK_TCK<<endl;
start=clock();
SelectSort(c,10000);
end=clock();
cout<<"The time was:"<<(double)(end - start) / CLK_TCK<<endl;
cout<<"\n快速排序结果为:";
/*start=clock();
QuickSort(num,0,size-1);
end=clock();
cout<<"The time was:"<<(double)(end - start) / CLK_TCK<<endl;*/
start=clock();
QuickSort(a,0,49);
end=clock();
cout<<"The time was:"<<(double)(end - start) / CLK_TCK<<endl;
start=clock();
QuickSort(b,0,999);
end=clock();
cout<<"The time was:"<<(double)(end - start) / CLK_TCK<<endl;
start=clock();
QuickSort(c,0,9999);
end=clock();
cout<<"The time was:"<<(double)(end - start) / CLK_TCK<<endl;
/* cout << "\n归并排序非递归算法排序结果为:" ;
start=clock();
MergeSort1(num, g1, size );
end=clock();
cout<<"The time was:"<<(double)(end - start) / CLK_TCK<<endl;
cout << "\n归并排序递归算法排序结果为:";
start=clock();
MergeSort2(num, h1,h2, 0,size-1 );
end=clock();
cout<<"The time was:"<<(double)(end - start) / CLK_TCK<<endl;*/
return 0;
}
各排序算法的实现程序未打出。
实验结果:
1、数组中的数据随机生成:随机数在1~9999999之间产生,数组的规模分别为50,100,1000,5000,10000时的结果如下:
2、数组中的数据已经是非递减有序。
数组的规模依次是
50,1000,10000
随机生成:
基本有序:
排序方法
数组规模
50 100 1000 5000 10000
所用时间
单位(s )
起泡 0 0 0.008 0.187 0.748
简单 0 0 0.003 0.084 0.328
快速
0 0 0.003 0.091 0.38
归并非递归
0.001
0.002
归并递归 0 0 0 0.001 0.003
排序方法数组规模50100010000
所用时间单位(s)
起泡00 0
简单00.0070.003
快速00.345 —
实验分析:1、在数据随机生成的情况下,归并非递归算法效率最好,起泡最差;
2、在数据基本有序的情况下,起泡排序最好(没考虑递归)
3、在数组规模为10000时,执行程序,有异常
产生处在快速排序处,想知道为什么??。