#include <iostream>
#include<>
using namespace std;
void BubbleSort(int a[],int n)
{
bool flag;
for(int i=0; i<n-1; i++)
{
flag=false;
for(int j=n-1; j>i; j--)
{
if(a[j-1]>a[j])
{
int temp;
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
flag=true;
cout<<endl;
for(int i=0; i<10; i++)
{
cout<<a[i]<<" ";
}
}
if(flag==false) return ;
}
}
int main()
{
int a[10];
for(int i=0; i<10; i++)
{
a[i]=rand()%30;
}
for(int i=0; i<10; i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
BubbleSort(a,10);cout<<endl;
for(int i=0; i<10; i++)
{
cout<<a[i]<<" ";
}
return 0;
}
冒泡排序C++ 来自淘豆网www.taodocs.com转载请标明出处.