/*****************************
*2011年10月18日16:37:38
*描述:用起泡法对十个数排序
*****************************/
# include <iostream>
using namespace std;
int main()
{
int a[10];
int i, j, t;
cout<<"inpur 10 numbers:"<<endl;
for(i = 0; i < 10; i++)//输入a[0]到a[9]
{
cin>>a[i];
}
cout<<endl;
for(j = 1; j <= 9; j++)//共进行9轮比较
{
for(i = 0; i < 10; i++)//在每轮中进行(10-j)次比较
if(a[i] > a[i+1])//把每两个数中较小的一个移到靠前的地方
{
t = a[i];
a[i] = a[i+1];
a[i+1] = t;
}
}
cout<<"the sorted numbers:"<<endl;
for(i = 0; i < 10; i++)//输出10个数
{
cout<<"a["<<i<<"] = "<<a[i]<<", ";
}
cout<<endl;
return 0;
}
/*************************************************************************************
*在vc++6.0中运行的结果是:
*----------------------------------------------
*inpur 10 numbers:
*1
*2
*3
*4
*5
*8
*7
*9
*10
*6
*
*the sorted numbers:
*a[0] = 1, a[1] = 2, a[2] = 3, a[3] = 4, a[4] = 5, a[5] = 6, a[6] = 7, a[7] = 8,
*a[8] = 9, a[9] = 10,
*-----------------------------------------------
**************************************************************************************/
c++_用起泡法对十个数进行排列
最新推荐文章于 2022-04-21 12:47:29 发布