冒泡排序(CPP)

#include<iostream>
#include<algorithm>
#include<string>


using namespace std;


void swap(int &a,int &b)
{
    int temp;
    temp=a;
    a=b;
    b=temp;
}
 void bubbleSort(int a[],int n)
 {
    for(int i=n-1;i>1;i--)
    {
        bool tag=false;
        for(int j=0;j<i;j++)
        {
            if(a[j]>a[j+1])
               {
                  tag=true;
                  swap(a[j],a[j+1]);
               }
        }
        if(tag=false)
        return;
    }
 }




int main()
{
    int a[8]={9,3,2,1,0,3,3,7};
    bubbleSort(a,8);
    for(int i=0;i<8;i++)
      cout<<a[i]<<"  ";
    cout<<endl;




}




你可能感兴趣的:(冒泡排序(CPP))