排序性能测试比较

来源:互联网 发布:java 获取当前locale 编辑:程序博客网 时间:2024/05/16 09:03

用了头文件“sort.h” 附链接: http://blog.csdn.net/hit_fantasy/article/details/8739985

用rand()生成随机数组,然后用start=clock();  XXXXSort(a,len);  finish=clock();来计时。

基数排序没有做,不知道怎么确定基数。

#include <iostream>#include <time.h>#include <cstdlib>#include "sort.h"#include <conio.h>#include <cstdio>using namespace std;void menu(){    cout<<"1.bubbleSort\n\n";    cout<<"2.selectSort\n\n";    cout<<"3.insertSort\n\n";    cout<<"4.shellSort\n\n";    cout<<"5.mergeSort\n\n";    cout<<"6.quickSort\n\n";    cout<<"7.radixSort\n\n";    cout<<"8.heapSort\n\n";    cout<<"9.exit\n\n";}void sortTime(){//creat the test array    srand(time(NULL));    //length 99999    cout<<"please input the length of the test array."<<endl;    int len;    cin>>len;    int *a=new int[len];   /* cout<<"please input max element in the array.(to ensure the scope)"<<endl;    int scope;    cin>>scope;*/    for(int i=0;i<len;i++)    {        a[i]=rand();        //a[i]=rand()%scope+1;    }//count the time    clock_t start,finish;    double duration;    cout<<"\n\nPlease chose 1-9 as follow.\n\n";    menu();    char in;    cin>>in;    switch(in)    {        case'1':            cout<<"bubbleSort:\n";            start=clock();            bubbleSort(a,len);            finish=clock();            duration=(double)(finish-start)/CLOCKS_PER_SEC;            printf("End! it costs %.3f seconds totally.\n",duration);            system("pause");            break;        case'2':            cout<<"selectSort:\n";            start=clock();            selectSort(a,len);            finish=clock();            duration=(double)(finish-start)/CLOCKS_PER_SEC;            printf("End! it costs %.3f seconds totally.\n",duration);            system("pause");            break;        case'3':            cout<<"insertSort:\n";            start=clock();            insertSort(a,len);            finish=clock();            duration=(double)(finish-start)/CLOCKS_PER_SEC;            printf("End! it costs %.3f seconds totally.\n",duration);            system("pause");            break;        case'4':            cout<<"shellSort:\n";            start=clock();            shellSort(a,len);            finish=clock();            duration=(double)(finish-start)/CLOCKS_PER_SEC;            printf("End! it costs %.3f seconds totally.\n",duration);            system("pause");            break;        case'5':            cout<<"mergeSort:\n";            start=clock();            mergeSort(a,len);            finish=clock();            duration=(double)(finish-start)/CLOCKS_PER_SEC;            printf("End! it costs %.3f seconds totally.\n",duration);            system("pause");            break;        case'6':            cout<<"quickSort:\n";            start=clock();            quickSort(a,len);            finish=clock();            duration=(double)(finish-start)/CLOCKS_PER_SEC;            printf("End! it costs %.3f seconds totally.\n",duration);            system("pause");            break;        case'7':            break;        case'8':            cout<<"heapSort:\n";            start=clock();            heapSort(a,len);            finish=clock();            duration=(double)(finish-start)/CLOCKS_PER_SEC;            printf("End! it costs %.3f seconds totally.\n",duration);            system("pause");            break;        case'9':            exit(0);    }    delete []a;}int main(){   // freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    while(1)    {        sortTime();    }    return 0;}
原创粉丝点击