You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
794 B
37 lines
794 B
//使用插入排序进行升序排列
|
|
#include <stdio.h>
|
|
int InsertSort(int A[], int n)
|
|
{
|
|
int i,j;
|
|
for(i=2; i<=n; i++)
|
|
{
|
|
A[0]=A[i];
|
|
for(j=i-1; A[0]<A[j]; j--)
|
|
{
|
|
A[j+1]=A[j];
|
|
}
|
|
A[j+1]=A[0];
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int main() {
|
|
int A[1024],n,i;
|
|
printf("请输入要输入的元素个数:");
|
|
scanf("%d",&n);
|
|
printf("\n请输入要排序的序列:\n");
|
|
for (i=1; i<=n; i++)
|
|
scanf("%d",&A[i]);
|
|
printf("\n使用直接插入排序算法后的结果:\n");
|
|
InsertSort(A,n);
|
|
for(i=1; i<=n; i++)
|
|
printf("%d\t",A[i]);
|
|
printf("\n");
|
|
return 0;
|
|
}
|
|
|
|
/*①时间复杂度为O(n^2)
|
|
②适用于顺序存储和链式存储(上面的代码是顺序存储)
|
|
③是稳定的排序算法
|
|
Process exited after 16.06 seconds with return value 0*/
|