#include <iostream>
#include <cstdlib>
#define length 10
using namespace std;
int num[length];
void print()
{
for(int i=0;i<length;++i)
{
cout<<num[i]<<' ';
}
cout<<endl;
}
int main()
{
for(int i=0;i<=length;++i)
{
num[i]=rand()%100+1;
}
cout<<"before:";
print();
for(int i=0;i<length-1;++i)
{
for(int j=0;j<length-i-1;j++)
{
if(num[j]>num[j+1])
{
num[j]+=num[j+1];
num[j+1]=num[j]-num[j+1];
num[j]=num[j]-num[j+1];
}
}
}
cout<<"after: ";
print();
return 0;
}
执行:
before:42 68 35 1 70 25 79 59 63 65
after: 1 25 35 42 59 63 65 68 70 79