| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 420 人关注过本帖
标题:希尔排序出错
只看楼主 加入收藏
最近不在
Rank: 8Rank: 8
等 级:蝙蝠侠
威 望:5
帖 子:204
专家分:842
注 册:2010-2-28
结帖率:95%
收藏
已结贴  问题点数:20 回复次数:1 
希尔排序出错
程序代码:
//插入排序
void sort1(int a[], int n)
{
    for(int i = 1; i != n; ++i)
    {
        //如果当前数字比相邻的数小
        if(a[i] < a[i-1])
        {
            int j;
            int temp = a[i];
            for(j = i-1; temp < a[j]; --j)
            {
                //数字右移
                a[j+1] = a[j];
            }
            a[j+1] = temp;
        }
    }
}

//希尔排序
void ShellInsert(int a[], int n, int d)
{
    for(int i = d; i != n; ++i)
    {
        ////如果当前数字比相邻的数小
        if(a[i] < a[i-d])
        {
            int j;
            int temp = a[i];
            for(j = i-d; j >= 0 && temp < a[j]; j -= d)
            {
                //元素右移
                a[j+d] = a[j];
            }
            a[j+d] = temp;
        }
    }
}

void ShellSort(int a[], int n)
{
    int d = n/2;
    while(d != 1)
    {
        //增量插入排序
        ShellInsert(a, n, d);
        d = d/2;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    int a[] = {7, 2, 5, 3, 6};

    int n = sizeof(a)/sizeof(*a);
    ShellSort(a, n);
    for(int i = 0; i != n; ++i)
    {
        cout<<a[i]<<endl;
    }

    return 0;
}

根据规则照着插入排序得出的代码,但排序结果不对,网上搜了下,貌似也差不多,求排错啊!
2010-12-30 00:35
lintaoyn
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:4
帖 子:606
专家分:2499
注 册:2009-4-8
收藏
得分:20 
程序代码:
void ShellSort(int a[], int n)
{
    int d = n/2;
    while(d)//最后的步长为1,就是得和普通插入一样
    {
        //增量插入排序
        ShellInsert(a, n, d);
        d = d/2;
    }
}

迭代的是人,递归的是神。
2010-12-30 08:13
快速回复:希尔排序出错
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025339 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved