冒泡排序
传统的冒泡排序:
#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
int a[5] = {3,7,4,5,2};
int i,j;
for (i = 0; i < 5; i++)
{
//下次只需循环5-1-i次
for (int j = 0; j < 5-1-i; j++)
{
//如果前面的数笔后面的数大,那么就互换值
if (a[j]>a[j+1]) {
int temp;
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for (int i = 0; i < 5; i++)
{
cout << a[i] << endl;
}
return 0;
}
/*
2
3
4
5
7
*/