#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
typedef int ElementType;
int n;
ElementType *a;
int src(void)
{
printf("src:\n");
srand((unsigned int)time(NULL));
for(int i = 0; i < n; i++) {
a[i] = rand() % 1000000;
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
int dst(void)
{
printf("dst:\n");
for(int i = 0; i < n; i++) {
printf("%d ", a[i]);
}
printf("\n");
return 0;
}
void InsertionSort(ElementType A[], int N)
{
int j,P;
ElementType Tmp;
printf("InsertionSort:A=%p N=%d\n", A, N);
for(P = 1; P < N; P++) {
//printf("%d ", P);
Tmp = A[P];
for(j = P; (j > 0) && (A[j-1] > Tmp); j--)
A[j] = A[j-1];
A[j] = Tmp;
}
//printf("\n");
return;
}
int main(void)
{
n = 10000;
a = (ElementType *)malloc(n * sizeof(ElementType));
src();
InsertionSort(a, n);
dst();
free(a);
return 0;
}
算法学习与实践之插入排序
最新推荐文章于 2022-08-14 21:56:50 发布