C++ 希尔排序两种实现及性能测试

希尔排序的实现思想,介绍等等不予赘述,网上非常多资料,直接贴出源码。

性能测试:
#include<iostream>
#include<algorithm>
#include<vector>
#include<ctime>
#include<cstdio>
using namespace std;
void ShellSort_2(vector<int> &v)
{
    int gap = v.size();
    do{
        gap = gap / 3 + 1;
        for(int i = gap; i < v.size(); ++i){
            for(int j = i-gap; j >= 0; j-=gap){
                if(v[j] > v[j+gap])
                    swap(v[j+gap], v[j]);
                else
                    break;
            }
        }
    }while(gap > 1);
    return ;
}

void ShellSort(vector<int> &v)
{
    for(int gap = v.size()/2; gap > 0; gap >>= 1){
        for(int i = gap; i < v.size(); ++i)
        {
            int val = v[i];
            int j=0;
            for(j = i-gap; j >= 0 && v[j] > val; j-=gap){
                v[j+gap] = v[j];
            }
            v[j+gap] = val;
        }
    }
    return ;
}

void InsertSort(vector<int> &v)
{
    for(int i = 1; i < v.size(); ++i){
        if(v[i] < v[i-1]){
            int j = i - 1;
            int t = v[i];
            while(j >= 0 && v[j] > t){
                v[j+1] = v[j];
                j--;
            }
            v[j+1] = t; // 最后一轮又执行了j--; 这里加一
        }
    }
    return ;
}
int main()
{
    vector<int> a, v;
    time_t start, over;
    double run_time;
    srand((unsigned)time(NULL));
    //for(int i = 10; i >= 0; --i) a.push_back(i);

    int scale = 0;
    for(scale = 500; scale <= 5e6; scale *= 10)
    {
        for(int i = scale; i >= 0; --i)
        {
            int t = (int)(1.0 * rand() / RAND_MAX * scale);
            a.push_back(t);
        }

        cout << "Original data :";
        for(int i = 0; i < 20; ++i)
        {
            cout << a[i] << ' ';
        }
        cout << endl;
        start = clock();
        //sort(a,a+scale,greater<int>());
        ShellSort_2(a);
        over = clock();
        cout << "Let's check some answer : " ;
        for(int i = 0; i < 20; ++i)
        {
            cout << a[i] << ' ';
        }
        cout << endl;
        printf("sort with %d scale lever costs %f s\n", scale, (double)(over - start)/CLOCKS_PER_SEC);
        a.clear();
    }
    for(scale = 500; scale <= 5e6; scale *= 10)
    {
        for(int i = scale; i >= 0; --i)
        {
            int t = (int)(1.0 * rand() / RAND_MAX * scale);
            v.push_back(i);
        }
        cout << "Original data :" ;
        for(int i = 0; i < 20; ++i)
        {
            cout << v[i] << ' ';
        }
        cout << endl;
        start = clock();
        //sort(a,a+scale,greater<int>());
        ShellSort(v);
        over = clock();
        cout << "Let's check some answer :" ;
        for(int i = 0; i < 20; ++i)
        {
            cout << v[i] << ' ';
        }
        cout << endl;
        printf("sort with %d scale lever costs %f s\n", scale, (double)(over - start)/CLOCKS_PER_SEC);
        v.clear();
    }

    return 0;

}


输出如下:


Original data :19 256 347 54 89 497 49 367 331 423 226 240 194 281 106 331 446 308 128 129
Let's check some answer : 0 2 3 4 6 7 10 10 12 12 12 15 17 17 18 19 19 20 20 21
sort with 500 scale lever costs 0.000000 s
Original data :3057 1586 2771 3673 771 3020 4276 3113 1465 1300 1180 3702 1188 1458 1327 589 972 3471 3079 1307
Let's check some answer : 1 2 3 3 3 4 4 4 6 8 10 11 12 12 13 13 14 16 16 19
sort with 5000 scale lever costs 0.002000 s
Original data :41613 28867 36558 33039 26215 49966 40441 44167 1545 16675 5652 22130 37974 23100 9283 32454 23520 26375 41285 46237
Let's check some answer : 0 0 1 4 4 6 6 7 10 10 12 13 13 13 15 15 16 16 18 18
sort with 50000 scale lever costs 0.025000 s
Original data :160222 172841 287270 321970 329798 482985 205999 101184 136738 375301 348384 439817 152729 244422 451551 112933 342707 74755 174871 54795
Let's check some answer : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 15 15
sort with 500000 scale lever costs 0.426000 s
Original data :2042603 3524735 620136 603808 1653950 3564256 959807 1969969 4121066 304574 1800439 803247 4933469 1800286 2907650 3521835 190282 1191747 3045289 1776177
Let's check some answer : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
sort with 5000000 scale lever costs 6.218000 s
Original data :500 499 498 497 496 495 494 493 492 491 490 489 488 487 486 485 484 483 482 481
Let's check some answer :0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
sort with 500 scale lever costs 0.000000 s
Original data :5000 4999 4998 4997 4996 4995 4994 4993 4992 4991 4990 4989 4988 4987 4986 4985 4984 4983 4982 4981
Let's check some answer :0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
sort with 5000 scale lever costs 0.001000 s
Original data :50000 49999 49998 49997 49996 49995 49994 49993 49992 49991 49990 49989 49988 49987 49986 49985 49984 49983 49982 49981
Let's check some answer :0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
sort with 50000 scale lever costs 0.012000 s
Original data :500000 499999 499998 499997 499996 499995 499994 499993 499992 499991 499990 499989 499988 499987 499986 499985 499984 499983 499982 499981
Let's check some answer :0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
sort with 500000 scale lever costs 0.147000 s
Original data :5000000 4999999 4999998 4999997 4999996 4999995 4999994 4999993 4999992 4999991 4999990 4999989 4999988 4999987 4999986 4999985 4999984 4999983 4999982 4999981
Let's check some answer :0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
sort with 5000000 scale lever costs 1.736000 s
关于时间测量:

我们使用头文件 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作比,就可以得到最后的秒数了。

总结:

希尔排序常见的实现方法有以上两种,根据测试,第一种运行效率是更高的。

希尔排序是不稳定排序,虽然组内是稳定,但很可能由于步长变化的原因导致不同组间的元素不再保持稳定。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值