| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1618 人关注过本帖
标题:希尔排序,想将排好的元素输出,但是数组传递有问题
只看楼主 加入收藏
Vsnow
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:124
专家分:145
注 册:2015-1-3
结帖率:95%
收藏
已结贴  问题点数:40 回复次数:4 
希尔排序,想将排好的元素输出,但是数组传递有问题
#include<iostream>
using namespace std;
#include <stdlib.h>
#include <iostream>
typedef int KeyType;
typedef char InfoType[10];
typedef struct
{   
    KeyType key;               
    InfoType data;              
} RecType;


/* 希尔排序 */
void shell(RecType R[],int n,int &moves,int &comtimes)
{
    int i,j,gap;
    RecType tmp;
    gap=n/2;    /* 增量置初值 */
    while(gap>0)
    {
        for(i=gap;i<n;i++)    /* 对所有相隔gap位置的元素组采用直接插入排序 */
        {
            tmp=R[i];
            moves++;/* 移动次数加一 */
            j=i-gap;
            while(j>=0 && tmp.key<R[j].key)/* 对相隔gap位置的元素组进行排序 */
            {
                R[j+gap]=R[j];
                j=i-gap;        
            }
            moves++;/* 移动次数加一 */
            comtimes++;/* 比较次数加一 */
            R[j+gap]=tmp;
            moves++;/* 移动次数加一 */
   
        }
        gap=gap/2;/* 减小增量 */
    }
/*    cout<<"从小到大排序:";
    for(int k=0;k<n;k++)
    {   
        cout<<R[k].key<<"   ";
    }*/
    cout<<"\n";
    cout<<"运用希尔排序所需比较次数为:"<<comtimes<<endl;
    cout<<"运用希尔排序所需移动次数为:"<<moves<<endl;
}

void main()
{   
    int a[5]={52,13,38,56,1,2};
    RecType b[10];
    for(int i=0;i<5;i++)
    {
        b[i].key=a[i];
    }
    int moves=0,comtimes=0;
    shell(b,5,moves,comtimes);
}
搜索更多相关主题的帖子: include 元素 
2015-12-27 13:26
yangfrancis
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:贵宾
威 望:141
帖 子:1510
专家分:7661
注 册:2014-5-19
收藏
得分:5 
什么叫希尔排序?
2015-12-28 12:17
wengbin
Rank: 10Rank: 10Rank: 10
来 自:陕西西安
等 级:贵宾
威 望:19
帖 子:370
专家分:1846
注 册:2015-5-8
收藏
得分:15 
不懂你在写什么,不过我调试了下,能跑了,但结果是不对的,所以就不贴代码了,简单说下,typedef char InfoType[10];这是要干嘛我不懂
程序代码:
while(j>=0 && tmp.key<R[j].key)/* 对相隔gap位置的元素组进行排序 */
     {
        R[j+gap]=R[j];
        j=i-gap;//当i=2,gap=2时,绝对死循环,走不出去了,应该是j=j-gap。        
    }

下面是一个能正确排序的,你看着加入你要的数据吧
程序代码:
#include<iostream>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
using namespace std;
typedef int KeyType;
typedef char InfoType;
typedef struct RecType
{
    KeyType key;
    InfoType data[10];
} RecType;


/* 希尔排序 */
void shellsort(RecType a[], int n)
{
    int i, j, gap;
    for (gap = n / 2; gap > 0; gap /= 2)
        for (i = gap; i < n; i++)
            for (j = i - gap; j >= 0 && a[j].key > a[j + gap].key; j -= gap)
                swap(a[j], a[j + gap]);

    cout<<"从小到大排序:";
    for(int k=0;k<n;k++)
    {
        cout<<a[k].key<<"   ";
    }
    cout<<"\n";

//    cout<<"运用希尔排序所需比较次数为:"<<comtimes<<endl;
//    cout<<"运用希尔排序所需移动次数为:"<<moves<<endl;
}

int main()
{
    int a[5]={52,13,38,56,12};
    RecType b[10];
    for(int i=0;i<5;i++)
    {
        b[i].key=a[i];
    }
    int moves=0,comtimes=0;
    shellsort(b,5);
    return 0;
}


[此贴子已经被作者于2015-12-28 15:47编辑过]

2015-12-28 15:23
诸葛欧阳
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:流年
等 级:贵宾
威 望:82
帖 子:2790
专家分:14619
注 册:2014-10-16
收藏
得分:5 
希尔排序写起来很复杂没尝试过,准备有时间写写试试

一片落叶掉进了回忆的流年。
2015-12-28 20:02
阿文fire
Rank: 2
等 级:论坛游民
威 望:1
帖 子:11
专家分:58
注 册:2009-7-7
收藏
得分:15 
回复 楼主 Vsnow
下面是我直接在你的代码上修改的,可以正确运行
程序代码:
#include<iostream>
using namespace std;
#include <stdlib.h>
//#include <iostream> 重复了
typedef int KeyType;
//typedef char InfoType[10]; 错误的类型定义
//typedef char InfoType; //正确定义
typedef struct
{
    KeyType key;
    //InfoType data[10];//根本未使用
} RecType;


/* 希尔排序 */
void shell(RecType R[],int n,int &moves,int &comtimes)
{
    int i,j,gap;
    RecType tmp;
    gap=n/2;    /* 增量置初值 */
    while(gap>0)
    {
        for(i=gap;i<n;i++)    /* 对所有相隔gap位置的元素组采用直接插入排序 */
        {
            tmp=R[i];
            //moves++;/* 移动次数加一 */ //这里没有移动
            j=i-gap;
            comtimes++;/* 比较次数加一 */ //不管移不移动都得先比较
            while(j>=0 && tmp.key<R[j].key)/* 对相隔gap位置的元素组进行排序 */
            {
                moves++;/* 移动次数加一 */
                R[j+gap]=R[j];
                j=j-gap;
            }
            //moves++;/* 移动次数加一 */ 没有移动
            //comtimes++;/* 比较次数加一 */
            R[j+gap]=tmp;
            //moves++;/* 移动次数加一 */ 没有移动

        }
        gap=gap/2;/* 减小增量 */
    }
    cout<<"从小到大排序:";
    for(int k=0;k<n;k++)
    {
        cout<<R[k].key<<"   ";
    }
    cout<<"\n";
    cout<<"运用希尔排序所需比较次数为:"<<comtimes<<endl;
    cout<<"运用希尔排序所需移动次数为:"<<moves<<endl;
}

//void main()
int main()
{
    //int a[5]={52,13,38,56,1,2}; 6个元素
    int a[5] = {52,13,38,56,1};
    RecType b[10];
    for(int i=0; i<5; i++)
    {
        b[i].key=a[i];
    }
    int moves=0, comtimes=0;
    shell(b,5,moves,comtimes);
    return 0;
}



[此贴子已经被作者于2015-12-28 23:16编辑过]

2015-12-28 23:12
快速回复:希尔排序,想将排好的元素输出,但是数组传递有问题
数据加载中...
 
   



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

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