1.冒泡算法
#include<iostream>
using namespace std;
int main()
{
int i, j, temp, a[5];
for (i = 0; i < 5; i++) cin >> a[i];
for (i = 0; i<5-1; i++) //n个数要进行n-1趟比较,为了简便,这里n=5
{
for (j = 0; j<5-i-1; j++) //每趟比较n-i-1次
if (a[j]>a[j+1]) //依次比较两个相邻的数,将小数放在前面,大数放在后面
{
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
for (i = 0; i < 5; i++) cout << a[i] << " "; //顺序输出
cout << endl;
for (i = 4; i >= 0; i--) cout << a[i]<<" "; //倒序输出
cout << endl;
return 0;
}