代码
#include<iostream>
#define N 9
#define ElemType int
/*输出数组*/
void OutPrint(ElemType A[]) {
int i;
for (i = 1; i <N; i++)
{
printf("%d ", A[i]);
}
}
/*直接插入排序*/
void InsertSort(ElemType A[], int n) {
int i, j;
for (i = 2; i <= n; ++i) {
if (A[i] < A[i - 1]) {
A[0] = A[i];
for (j = i - 1; A[0] < A[j]; --j)
A[j + 1] = A[j];
A[j + 1] = A[0];
}
}
}
int main() {
ElemType A[N] = {-1,48, 62, 35, 77, 55,14,35,98 };
printf("排序前数组
");
OutPrint(A);
InsertSort(A, N-1);
printf("
排序后数组
");
OutPrint(A);
system("pause");
return 0;
}
运行结果如图: