插入排序就是每一步都将一个待排数据按其大小插入到已经排序的数据中的适当位置,直到全部插入完毕。
#include<stdio.h>
int main()
{
int n,i,j,k,a[100];
printf("Please in put n:\n");
scanf("%d",&n);
printf("Please in put %d number:\n",n);
for(k=1;k<=n;k++)
scanf("%d",&a[k]);
for(i=2;i<=n;i++)
{
a[0]=a[i];
j=i-1;
while(a[0]<a[j])
{
a[j+1]=a[j];
j--;
}
a[j+1]=a[0];
}
printf("After insert sort the arrary is:\n");
for(i=1;i<=n;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
/*
Please in put n:
6
Please in put 6 number:
7 4 -2 19 13 6
After insert sort the arrary is:
-2 4 6 7 13 19
请按任意键继续. . .*/
int main()
{
int n,i,j,k,a[100];
printf("Please in put n:\n");
scanf("%d",&n);
printf("Please in put %d number:\n",n);
for(k=1;k<=n;k++)
scanf("%d",&a[k]);
for(i=2;i<=n;i++)
{
a[0]=a[i];
j=i-1;
while(a[0]<a[j])
{
a[j+1]=a[j];
j--;
}
a[j+1]=a[0];
}
printf("After insert sort the arrary is:\n");
for(i=1;i<=n;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
/*
Please in put n:
6
Please in put 6 number:
7 4 -2 19 13 6
After insert sort the arrary is:
-2 4 6 7 13 19
请按任意键继续. . .*/