排序性能测试比较

用了头文件“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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值