用C++,C语言实现几种排序以及性能对比(sort,qsort,冒泡)

性能测试:
#include <iostream>
#include <set>
#include <cstdio>
#include <ctime>
#include <algorithm>
//typedef long long ll;
#define ll long long
#define MAXN 1000009
using namespace std;
struct box_Sort
{
    int score;
    string name;
}my_Box[100];
int a[1000009];

void bubble_Sort(int scale)
{
    for(int i = 0; i < scale; ++i)
        for(int j = 0; j < scale - i; ++j)
            if(a[j] > a[j + 1])
                swap(a[j],a[j + 1]);
}


int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

void box_sort(int a[MAXN])
{
    int num;
    int t;
    cin >> num;
    while(num--)
    {
        cin >> t;
        a[t] ++;
    }
    for(int i = 0; i < MAXN; ++i)
        if(a[i])
            cout << i << ' ';
}
void bubble_sort(int a[MAXN])
{
    for(int i = 0; i < MAXN - 1; ++i)
        for(int j = 0; j < MAXN - 1 - i; ++j)
            if(a[j] > a[j + 1]) // descent descend ascent ascend
                swap(a[j], a[j + 1]);
}
void quick_sort(int left, int right)
{
    if(left > right)
        return ;
    int temp = a[left];
    int i = left, j = right;
    while(i != j)
    {
        while(a[j] >= temp && j > i)
            j--;
        while(a[i] <= temp && i < j)
            i++;
        if(i < j)
        {
            swap(a[i], a[j]);
        }
    }
    a[left] = a[i];
    a[i] = temp;
    quick_sort(left, i - 1);
    quick_sort(i + 1, right);
    return ;
}
int main(){
    time_t start, over;
    double run_time;
    int i;
    int scale = 100;
    for(scale; scale <= 1e4; scale *= 10)
    {
        for(i = 0; i <= scale; i++)
            a[i] = i;
        start = clock();
        bubble_Sort(scale);
        over = clock();
        printf("bubble with %d scale lever costs %f s\n", scale, (double)(over - start)/CLOCKS_PER_SEC);
    }
    for(scale = 100; scale <= 1e6; scale *= 10)
    {
        for(i = 0; i <= scale; i++)
            a[i] = i;
        start = clock();
        //QuickSort(0,scale);
        qsort(a, scale, sizeof(int), compare);
        over = clock();
        printf("qsort with %d scale lever costs %f s\n", scale, (double)(over - start)/CLOCKS_PER_SEC);
    }
    for(scale = 100; scale <= 1e6; scale *= 10)
    {
        for(i = 0; i <= scale; i++)
            a[i] = i;
        start = clock();
        sort(a,a+scale,greater<int>());
        over = clock();
        printf("sort with %d scale lever costs %f s\n", scale, (double)(over - start)/CLOCKS_PER_SEC);
    }
    for(scale = 40; scale <= 40000; scale *= 10)
    {
        for(i = 0; i <= scale; i++)
            a[i] = i;
        start = clock();
        quick_sort(0, scale);
        //qsort(a, scale, sizeof(int), compare);
        over = clock();
        printf("my_qsort with %d scale lever costs %f s\n", scale, (double)(over - start)/CLOCKS_PER_SEC);
    }
    cout << "Mission complete";
    return 0;
}

输出如下:


bubble with 100 scale lever costs 0.000000 s
bubble with 1000 scale lever costs 0.003000 s
bubble with 10000 scale lever costs 0.308000 s
qsort with 100 scale lever costs 0.000000 s
qsort with 1000 scale lever costs 0.001000 s
qsort with 10000 scale lever costs 0.001000 s
qsort with 100000 scale lever costs 0.012000 s
qsort with 1000000 scale lever costs 0.153000 s
sort with 100 scale lever costs 0.000000 s
sort with 1000 scale lever costs 0.001000 s
sort with 10000 scale lever costs 0.002000 s
sort with 100000 scale lever costs 0.033000 s
sort with 1000000 scale lever costs 0.370000 s
my_qsort with 40 scale lever costs 0.000000 s
my_qsort with 400 scale lever costs 0.001000 s
my_qsort with 4000 scale lever costs 0.049000 s
my_qsort with 40000 scale lever costs 4.790000 s
Mission complete
关于时间测量:

我们使用头文件 ctime 来包含我们所需要的函数,完成计数。

	time_t start, over;
	double run_time;
		start = clock();
        function();
        over = clock();
        printf("my_qsort with %d scale lever costs %lf s\n", scale, (double)(over - start)/CLOCKS_PER_SEC);

time_t start, over;time_t 是在中的一个结构体。

start = clock() 调用这样的语句,在一段我们需要计数的函数体两端,减去之后即可得到一个点数。

但想得到用秒为单位的时间,需要先将其转化为double类型,然后与CLOCKS_PER_SEC作比,就可以得到最后的秒数了。

总结:

根据数据量的增加, 我们不难发现:

qsort > sort > my_qsort = bubble

考虑原因:qsort 比 sort 更快是由于优化问题,sort并没有采用最佳最高效的排序策略,但其实也够用了。如果非要追求效率,那么使用qsort。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值