代码
#include<iostream>
#define N 8
#define ElemType int
/*冒泡排序*/
void InsertSort(ElemType A[],int n) {
int i,j,temp;
bool flag;
for (i = 0; i <= n; ++i) {
flag = false;
for (j = n - 1; j > i; --j)
if (A[j - 1] > A[j]) {
temp = A[j];
A[j] = A[j - 1];
A[j - 1] = temp;
flag = true;
}
if (flag == false)
return;
}
}
/*输出数组*/
void OutPrint(ElemType A[]){
int i;
for (i = 0; i < N; i++)
{
printf("%d ", A[i]);
}
}
int main() {
ElemType A[N] = { 48, 62, 35, 77, 55,14,35,98 };
printf("排序前数组
");
OutPrint(A);
InsertSort(A, N);
printf("
排序后数组
");
OutPrint(A);
system("pause");
return 0;
}
结果如图: