哈希插入排序

#include <stdio.h>
#include <iostream>
#include <stdlib.h>

using namespace std;

#define MAXSIZE 20
#define OK 0
#define ERROR 1

typedef char InforType;
typedef int KeyType;
typedef int Status;
typedef struct {
    KeyType key;
    InforType otherinfo;
}RedType;
typedef struct{
    RedType r[MAXSIZE+1];
    int Length;
}SqList;

/* 初始化 */
Status Initialize(SqList &L){
    L.Length=0;
    return OK;
}

/* 存储数据 */
Status StoreData(SqList &L){
    int i;
    int num;
    /* cout<<"Please input the num of data:"; */
    cin>>num;
    if(num>MAXSIZE) exit(ERROR);
    /* cout<<"Please input data:"<<endl; */
    for(i=1;i<num+1;i++){
        cin>>L.r[i].key;
    }
    L.Length=num;                         
    return OK;
}

/* 输出数据 */
Status OutPut(SqList L){
    int i;
    if(L.Length==0) exit(ERROR);
    /* cout<<"The data of L:"<<endl; */
    for(i=1;i<L.Length+1;i++){
        cout<<L.r[i].key<<" ";
    }
    cout<<"\n";
    return OK;
}

/* 希尔排序 */
void ShellInsert(SqList &L,int dk){
    int i,j;
    for(i=dk+1;i<=L.Length;i++){
        if(L.r[i].key<L.r[i-dk].key){
            L.r[0]=L.r[i];
            for(j=i-dk;j>0&&L.r[0].key<L.r[j].key;j-=dk){
                L.r[j+dk]=L.r[j];
            }
            L.r[j+dk]=L.r[0];
        }
    }
    OutPut(L);
}

void ShellSort(SqList &L,int dt[],int t){
    int k;
    for(k=0;k<t;k++){
        ShellInsert(L,dt[k]);
    }
}

/* 主函数 */
int main(){
    SqList L;
    int t;
    int dt[3]={5,3,1};
    t=3;
    Initialize(L);
    StoreData(L);
   /*  OutPut(L); */
    ShellSort(L,dt,t);
    /* cout<<"After sort:"<<endl; */
    /* OutPut(L); */
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值