#include
#include
#include
const int MAXSIZE=100;
typedef int KeyType;
typedef int ElemType;
typedef struct
{ KeyType key;
ElemType data;
}SqType;
void InsertSort(SqType R[],int n)
{ int i,j;
SqType tmp;
for(i=1;i
if(R[i-1].key>R[i].key)
{ tmp=R[i];
j=i-1;
do
{ R[j+1]=R[j];
j--;
}while(j>=0 && R[j].key>tmp.key);
R[j+1]=tmp;
}
printf("i=%d",i); for(int i=0; i<7; i++)printf("%3d",R[i].key);printf("n");
}
}
void ShellSort(SqType R[],int n)
{ int i,j,d;
SqType tmp;
d=n/2;
while(d>0)
{ for(i=d;i
j=i-d;
while(j>=0 && tmp.key
j=j-d;
}
R[j+d]=tmp;
}
printf("i=%d",d); for(int i=0; i<7; i++)printf("%3d",R[i].key);printf("n"); d=d/2;
}
}
void BubbleSort(SqType R[],int n)
{ int i,j,exchang;
SqType tmp;
for(i=0;i
exchang=0;
for(j=n-1;j>i;j--)
if(R[j].key
R[j]=R[j-1];
R[j-1]=tmp;
exchang=1;
}
printf("i=%d",i); for(int x=0; x<7; x++)printf("%3d",R[x].key);printf("n");
if(exchang==0)
return;
}
}
void main()
{ SqType R[MAXSIZE];
int a[]={47,56,29,16,92,33,20},i;
printf("初始序列:");
for(i=0; i<7; i++)
{
R[i].key=a[i];printf(" %d ",R[i].key);
}
printf("n直接插入排序:n");
InsertSort(R,7);
printf("初始序列:");
for(i=0; i<7; i++)
{
R[i].key=a[i];printf(" %d ",R[i].key);
}
printf("n希尔排序:n");
ShellSort(R,7);
printf("初始序列:");
for(i=0; i<7; i++)
{
R[i].key=a[i];printf(" %d ",R[i].key);
}
printf("n冒泡排序:n");
BubbleSort(R,7);
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)