当前位置:文档之家› 实现一个排序算法并统计其运行时间

实现一个排序算法并统计其运行时间

#include<iostream> #include <algorithm> #include<stdlib.h> #include<dos.h> #include<time.h> #include<stdlib.h> #include<time.h> #include<fstream> using namespace std; class Array { protected:
ofstream ofs2("c:\\排序之后的数组(直接插入排序).txt",ios::app);//把排序之后的数组,
写进 c:\\排序之后的数组(直接插入排序).txt 中
for(i=0;i<n;i++)
{
ofs2<<a.GetArray(i)<<" ";
if((i+1)%5==0)
ofs2<<endl;
void Array::OutArray() {
for(int i=0;i<n;i++) {
if((i+1)%6==0)//每六个数换一次行 cout<<endl;
cout<<"a["<<i<<"]="<<a[i]<<" "; //输出 a[i]
} cout<<endl; }
void Array::QuickSort(int left,int right) {
a.Test();
totaltime=(double)(finsh-start)/CLOCKS_PER_SEC;
cout<<"\n 直接插入排序所花的 CPU 时间为"<<totaltime<<"秒!"<<endl;
ofstream ofs1("c:\\直接插入排序所花的 CPU 时间.txt",ios::app);//把直接插入排序所花的
break;
case 1:
Function();
break;
case 2:
Function1();
break;
}
}
void main()
{
Run();
}
{
ofs<<a.GetArray(i)<<" ";
if((i+1)%5==0)
ofs<<endl;
}
ofs<<" "<<endl;
// a.OutArray();/*显示排序之前的数组元素*/
start=clock();
a.InsertSort();
///!!!!!!!!!!!!!!
finsh=clock();
CPU 时间,写进 c:\\直接插入排序所花的 CPU 时间.txt 中
ofs1<<"规模为"<<n<<"直接插入排序所花的 CPU 时间为"<<totaltime<<"秒"<<"排序是
正确(true(1)/false(0))"<<test<<endl;
// a.OutArray();/*显示排序之后的数组元素*/
n=x; return n; } int GetArray(int i){return a[i];} void OutArray(); void QSort(){QuickSort(0,n-1);} void QuickSort(int left,int right);//快速排序 //void InSort(){InsertSort({1,2,3,4,5}, n);} bool Test();
ofs2<<a.GetArray(i)<<" ";
if((i+1)%5==0)
ofs2<<endl;
}
ofs2<<" "<<endl;
}
void Function1()
{
int n;//自定义数组长度
bool test;
clock_t start,finsh;//统计 CUP 时间的变量
double totaltime;//最终 CUP 所用的时间
ofs<<a.GetArray(i)<<" "; if((i+1)%5==0)
ofs<<endl; }
ofs<<" "<<endl; // a.OutArray();/*显示排序之前的数组元素*/ start=clock(); a.QSort(); finsh=clock(); a.Test(); totaltime=(double)(finsh-start)/CLOCKS_PER_SEC; cout<<"\n 快速排序所花的 CPU 时间为"<<totaltime<<"秒!"<<endl; ofstream ofs1("c:\\快速排序所花的 CPU 时间.txt",ios::app);//把快速排序所花的 CPU 时 间,写进 c:\\快速排序所花的 CPU 时间.txt 中 ofs1<<"规模为"<<n<<"快速排序所花的 CPU 时间为"<<totaltime<<"秒"<<"排序是正确 (true(1)/false(0))"<<test<<endl; // a.OutArray();/*显示排序之后的数组元素*/ ofstream ofs2("c:\\排序之后的数组(快速排序).txt",ios::app);//把排序之后的数组,写进 c:\\排序之后的数组(快速排序).txt 中 for(i=0;i<n;i++) {
int i,j; if(left<right) {
i=left; j=right+1; do {
do i++; while(a[i]<a[left]); do j--; while(a[j]>a[left]); if(i<j)
swap(a[i],a[j]); }while(i<j); swap(a[left],a[j]); QuickSort(left,j-1);//对低端序列快速排序 QuickSort(j+1,right);//对高端序列快速排序 } }//快速排序
****"<<endl; 排序 ****"<<endl;
cout<<"****2.直接插入 排序 ****"<<endl;
cout<<"**************************"<<endl; cout<<"请选择排序的类型:"<<endl;
cin>>ch;
switch(ch)
{
case 0:
void InsertSort(); //直接插入排序
}; void Array::creat() {
if(n<=100000) { srand( (unsigned)time( NULL ) ); for(int i=0;i<n;i++) {
a[i]=rand()%n;//随机产生 0-(n-1)之间的数 } } else {
cout<<"请输入数组长度:";
cin>>n;
Array a(n,2);//定义 Array 类对象,实现数组的创建
ofstream ofs("c:\\排序之前的数组(直接插入排序).txt",ios::app);//把随机产生的数组,
写进 c:\\排序之前的数组(直接插入排序).txt 中
for(int i=0;i<n;i++)
}Байду номын сангаас
ofs2<<" "<<endl;
}
void Run()
{
system("color 1f");
int ch;
cout<<endl;
cout<<"**************************"<<endl;
cout<<"****
排序系统 ****"<<endl;
cout<<"****0.退出 cout<<"****1.快速
1.实现一个排序算法并统计其运行时间。包括:1) 实现一个排序算法;2) 利 用事后统计法观察其时间复杂度: 对于每组待排序记录,统计你的排序算法 排序所花的CPU时间。对于不同规模的输入,统计相应的CPU时间,并作出时 间随规模变化的统计图(利用某种方法生成足够大的输入, 将输入和输出存 到文件中以便于观察。);3) 你是如何确保你的排序是正确的?能否给出一个 测试排序正确性的函数?
相关主题