冒泡法排序

来源:互联网 发布:java实现记事本功能 编辑:程序博客网 时间:2024/06/06 07:32

 #include <iostream.h>
void sort(int[],int);
void main()
{
 int b[9]={71,83,52,35,100,91,8,45,1};
 cout<<"原始数据:"<<endl;
 for(int i=0;i<9;i++)cout<<b[i]<<"\t";
 cout<<endl;
 sort(b,9);
 cout<<"排序后的数据为:"<<endl;
 for(i=0;i<9;i++) cout<<b[i]<<"\t";
 cout<<endl;
}
void sort(int a[],int len)
{
 int temp,i,j,flag;
 for(i=1;i<len;i++)
 {
  flag=1;
  for(j=0;j<len;j++)
  {
   if(a[j]>a[j+1])
   {
    flag=0;
    temp=a[j];
    a[j]=a[j+1];
    a[j+1]=temp;
   }
  }
  if(flag==1)break;

原创粉丝点击