插入排序,对于少量元素的排序,是一个有效的算法。将其伪代码过程命名为INSERTION-SORT,其中的参数是一个数组A[1..n],包含长度为n的要排序的一个序列。
伪代码如下:
INSERTION-SORT(A)
for j = 2 to A.length
key = A[j]
// Insert A[j] into thesorted sequence A[1..j - 1]
i = j – 1
while i > 0 and A[j]> key
A[i+1] = A[i]
i = i – 1
A[i+1] = key
代码实现如下:
#include <stdio.h>
void main()
{
inta[]={2,3,9,4,1,8,5,7,6,0};
inti,j,temp;
for(i=1;i<10;i++)
{
temp=a[i];
j=i-1;
while(j>=0&& a[j]>temp)
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("sortnumber:\n");
for(i=0;i<10;i++)
{
printf("%4d",a[i]);
}
printf("\n");
}
1147

被折叠的 条评论
为什么被折叠?



