cpp
//冒泡排序
#include<iostream>
using namespace std;
int main() {
int arr2[] = {4,2,8,0,5,7,1,3,9};
cout << "排序前:" << endl;
for (int i = 0; i < 9;i++) {
cout << arr2[i] << " ";
}
cout << endl;
//开始冒泡排序
//总共排序论述为 元素个数 -1
for (int i = 0;i < 9 - 1; i++)
{
//内层循环对比 次数=元素个数-1-当前轮数
for (int j = 0; j < 9 - 1 - i; j++)
{
//如果第一个数字,比第二个数字大,交换两个数字
if (arr2[j] > arr2[j + 1])
{
int temp = arr2[j];
arr2[j] = arr2[j + 1];
arr2[j + 1] = temp;
}
}
}
cout << "排序后:" << endl;
for (int i = 0; i < 9; i++) {
cout << arr2[i] << " ";
}
cout << endl;
system("pause");
return 0;
}