#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); */
}
哈希插入排序
最新推荐文章于 2023-04-01 20:49:07 发布