c++冒泡排序
作者:互联网
#include<iostream>
using namespace std;
int main()
{
//利用,冒泡排序进行升序
int a[5] = { 6,9,3,7,5 };
cout << "排序前" << endl;
for (int i = 0; i < 5; i++)
{
cout << a[i] << endl;
}
//开始冒泡排序
//总共排序轮数为元素数-1
for (int i = 0; i < 5; i++)
{
//内层循环对比,次数=元素数-1-轮数
for (int j = 0; j < 5 - 1 - i; j++)
if (a[j] > a[j + 1])
{
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
cout << "排序后" << endl;
for (int k= 0; k < 5; k++)
{
cout << a[k] << endl;
}
return 0;
}
标签:cout,temp,int,c++,++,冒泡排序,轮数 来源: https://blog.csdn.net/qq_54473522/article/details/123241514