一列数字,通过比较互相交换,让最大的值排到最后,需要排序的数组长度减一;递归;
#include<iostream>
#include<vector>
using namespace std;
void bSort(int a[], int length)
{
for (int pos = length-1; pos >0; pos--)//不断减小数组长度;
{
for (int begin = 0; begin < pos; begin++)
{
if (a[begin]>a[begin +1])
{
std::swap(a[begin], a[begin + 1]);//让最大值往后排,排到最后;
}
}
}
}
int main()
{
int a[10] = { 12,88,72,45,3,15,7,66,92,31 };
bSort(a, 10);
for (auto const& item:a)
{
cout << item << endl;
}
return 0;
}