8640
时间限制:1000MS
题型:编程题
Sample Input10 5 4 8 0 9 3 2 6 7 1
Sample Output
3 2 6 0 1 5 4 8 7 9
1 0 3 2 4 5 6 8 7 9
0 1 2 3 4 5 6 7 8 9
Hint
作者 yqm
答案:
#include<stdio.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define ElemType int
typedef struct
{
int *elem;
int length;
}SqList;
int InitList_Sq(SqList &L,int m)
{
L.elem=(ElemType*)malloc((m+1)*sizeof(ElemType));
if(!L.elem)
return ERROR;
L.length=m;
return OK;
}
int Load_Sq(SqList &L)
{
int i;
if(L.length==0)
printf("The List is empty!");
else
{
for(i=1;i<=L.length;i++)
printf("%d ",L.elem[i]);
}
printf("\n");
return OK;
}
void ShellInsert(SqList &L,int dk)
{
int i,j;
for(i=dk+1;i<=L.length;i++)
{
if(L.elem[i]<L.elem[i-dk])
{
L.elem[0]=L.elem[i];
for(j=i-dk;j>0&&L.elem[0]<L.elem[j];j-=dk)
L.elem[j+dk]=L.elem[j];
L.elem[j+dk]=L.elem[0];
}
}
}
void ShellSort(SqList &L,int m)
{
for(m=m/2;m>=1;m=m/2)
{
ShellInsert(L,m);
Load_Sq(L);
}
}
int main()
{
SqList T;
int m,i;
ElemType x;
scanf("%d",&m);
if(InitList_Sq(T,m)) // 判断顺序表是否创建成功
{
for(i=1;i<=m;i++)
{
scanf("%d",&x);
T.elem[i] = x;
}
}
ShellSort(T,m);
return 0;
}