#include<iostream>
#include<cstring>
using namespace std;
#define max 100
int i,j,k,low,high,mid;
typedef struct {
string name;
int chengji;
} student;
typedef struct {
student s[max];
int length;
} sqlist;
void creatsqlist(sqlist &S) { //输入要排序的数字
for(i=1; i<=S.length; i++)
cin>>S.s[i].chengji;
}
void insertsort(sqlist &S) { //直接插入排序
for(i=2; i<=S.length; i++) {
if(S.s[i].chengji<S.s[i-1].chengji) {
S.s[0]=S.s[i];
S.s[i]=S.s[i-1];
for(j=i-2;S.s[j].chengji>S.s[0].chengji;j--)
S.s[j+1]=S.s[j];
S.s[j+1]=S.s[0];
}
}
}
void Binsertsort(sqlist &S) //折半插入排序
{
for(i=2;i<=S.length;i++)
{
S.s[0]=S.s[i];
low=1;
high=i-1;
while(low<=high)
{
mid=(low+high)/2;
if(S.s[mid].chengji>S.s[0].chengji)high=mid-1;
else
low=mid+1;
}
for(j=i-1;j>=high+1;j--)
S.s[j+1]=S.s[j];
S.s[high+1]=S.s[0];
}
}
void shellinsert(sqlist &S,int dk)
{
for(i=dk+1;i<=S.length;i++)
{
if(S.s[i].chengji<S.s[i-dk].chengji)
{
S.s[0].chengji=S.s[i].chengji;
for(j=i-dk;j>0&&S.s[j].chengji>S.s[0].chengji;j=j-dk)
S.s[j+dk]=S.s[j];
S.s[j+dk]=S.s[0];
}
}
}
void shellsort(sqlist &S)
{
int d = S.length / 2; //初始增量设为数组长度的一半
while(d >= 1)
{
shellinsert(S,d);
d = d / 2; //每次增量变为上次的二分之一
}
}
void printfsqlist(sqlist S)
{
for(i=1;i<=S.length;i++)
printf("%d ",S.s[i].chengji);
printf("\n");
}
int main()
{
sqlist S;
printf("请输入数字数量:\n");
cin>>S.length;
printf("输入要排序的数字:\n");
creatsqlist(S);
printf("直接插入排序结果:\n");
insertsort (S);
printfsqlist(S);
printf("输入要排序的数字:\n");
creatsqlist(S);
printf("折半插入排序结果:\n");
Binsertsort(S);
printfsqlist(S);
printf("输入要排序的数字:\n");
creatsqlist(S);
printf("希尔排序结果:\n");
shellsort(S);
printfsqlist(S);
}