//将待排序记录放入监视哨,与已排好的比较并插入
#include <stdio.h>
#define MAX 11
typedef struct
{
int key;
char name;
}elemtype;
void insertsort(elemtype x[],int n);
main()
{
elemtype x[MAX];
int n,i;
printf("\nPlease input the number of data,n=");
scanf("%d",&n);
printf("\nplease input the data to be sorted:");
for(i=1;i<=n;i++)
scanf("%d",&x[i].key);
printf("The original data is:\t");
for(i=1;i<=n;i++)
printf("%d\t",x[i].key);
printf("\n\n");
insertsort(x,n);
printf("\n\n");
printf("The fianl result is:\t ");
for(i=1;i<=n;i++)
printf("%d\t",x[i].key);
}
void insertsort(elemtype x[],int n)
{
int i,j,m,k;
for(i=2;i<=n;i++)
{
x[0]=x[i];
j=i-1;
while(x[0].key<x[j].key)
{
x[j+1]=x[j];
j--;
}
x[j+1]=x[0];
m=i-1;
printf("\nNo.%d insert sort result\t",m);
for(k=1;k<=n;k++)
printf("%d\t",x[k].key);
}
}