#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
/*自己预测
5,4,3,2,1
4,3,2,1,5 i = 0 j < 4
3,2,1,4,5 i = 1 j < 3
2,1,3,4,5 i = 2 j < 2
1,2,3,4,5 i = 3 j < 1
*/
void print_array(int a[], int i)
{
for (int j = 0; j < i; j++)
{
cout << a[j] << ",";
}
cout << endl;
}
int main()
{
int a[5] = {5,4,3,2,1};
int i, j, temp;
cout << a[5] << endl;
print_array(a, 5);
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4-i; j++)
{
if (a[j] > a[j+1])
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
print_array(a, 5);
}
return 0;
}
这里发现用codeblock数组越界不会报错