#define N 10 //定义一个宏,赋值为10
void insertion_sort(int x[], int n) //根据算法导论的伪代码演变的方法
{
int i, j, key;
for (i=1; i
{
key=x[i];
j = i-1;
while ((j>=0) && (x[j]> key))
{
x[j+1] = x[j];
j--;
}
x[j+1] = key;
}
}
main()
{
int a[N],i,j;
printf("enter the numbers to be sorted:",N);
for(i=0;i
scanf("%d",&a[i]);
insertion_sort(a,N);
printf("the sorted numbers are: ");
for(i=0;i
{
printf(" %d ",a[i]);
}
printf("\n");
return 0;
}
没有评论:
发表评论