|
|
@ -1,4 +1,4 @@
|
|
|
|
//ʹ<EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
//使用插入排序进行升序排列
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
int InsertSort(int A[], int n)
|
|
|
|
int InsertSort(int A[], int n)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -17,12 +17,12 @@ int InsertSort(int A[], int n)
|
|
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
int main() {
|
|
|
|
int A[1024],n,i;
|
|
|
|
int A[1024],n,i;
|
|
|
|
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
|
|
|
printf("请输入要输入的元素个数:");
|
|
|
|
scanf("%d",&n);
|
|
|
|
scanf("%d",&n);
|
|
|
|
printf("\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>\n");
|
|
|
|
printf("\n请输入要排序的序列:\n");
|
|
|
|
for (i=1; i<=n; i++)
|
|
|
|
for (i=1; i<=n; i++)
|
|
|
|
scanf("%d",&A[i]);
|
|
|
|
scanf("%d",&A[i]);
|
|
|
|
printf("\nʹ<EFBFBD><EFBFBD>ֱ<EFBFBD>Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>㷨<EFBFBD><EFBFBD>Ľ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>\n");
|
|
|
|
printf("\n使用直接插入排序算法后的结果:\n");
|
|
|
|
InsertSort(A,n);
|
|
|
|
InsertSort(A,n);
|
|
|
|
for(i=1; i<=n; i++)
|
|
|
|
for(i=1; i<=n; i++)
|
|
|
|
printf("%d\t",A[i]);
|
|
|
|
printf("%d\t",A[i]);
|
|
|
@ -30,7 +30,7 @@ int main() {
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*<EFBFBD><EFBFBD>ʱ<EFBFBD>临<EFBFBD>Ӷ<EFBFBD>ΪO(n^2)
|
|
|
|
/*①时间复杂度为O(n^2)
|
|
|
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><EFBFBD>洢<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD>洢(<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˳<EFBFBD><EFBFBD>洢)
|
|
|
|
②适用于顺序存储和链式存储(上面的代码是顺序存储)
|
|
|
|
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>㷨
|
|
|
|
③是稳定的排序算法
|
|
|
|
Process exited after 16.06 seconds with return value 0*/
|
|
|
|
Process exited after 16.06 seconds with return value 0*/
|
|
|
|