#include<iostream>
using namespace std;
int main()
{
int a[5];
int temp;
bool swapped;
int top=5,bottom=0,bound=0;
int i,j,k,t=0;
for(i=0;i<5;i++)
cin>>a[i];
swapped=true;
while(swapped)
{
cout<<"__________________________________"<<endl;
cout<<"loading "<<t+1<<" times"<<endl;
swapped=false;
for(i=bottom;i<top;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
for(k=0;k<5;k++)
printf("up:%d ",a[k]);
printf("\n");
swapped=true;
bound=i;
}
top=bound;
for(j=top;j>bottom;j--)
{
if(a[j]<a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
for(k=0;k<5;k++)
printf("dw:%d ",a[k]);
printf("\n");
swapped=true;
bound=j;
}
bottom=bound;
t++;
}
for(k=0;k<5;k++)
printf("最终:%d ",a[k]);
printf("\n");
//system("pause");
return 0;
}
已整理代码,结合运行结果看代码,运行过程很清晰。