#17363: C++氣泡排序法 清楚明瞭


052026@hchs.kh.edu.tw (徐怡娟)


#include <iostream>
using namespace std ;

int main()
{
    int a[1000]={};
    int N , temp ;

    while(cin >>N)
    {
        for(int i=0; i<N; i++)
            cin >> a[i] ;
            cout<< endl ;
       

       for(int i=0; i<N-1; i++)
       {
           for(int j=0; j<N-1-i; j++)
          {
             if(a[j]>=a[j+1])
             {
                 temp=a[j+1] ;
                 a[j+1]=a[j] ;
                 a[j]=temp ;
              }
          }
       }
       for(int i=0; i<N; i++)

           cout<< a[i] << " ";
           cout<< endl ;
    }
return 0 ;
}

#17380: Re:C++氣泡排序法 清楚明瞭


ufve0704 (爬 我爬 我爬爬爬 有排行榜這種東西就是要爬 爬過我上面的那...)


#include
using namespace std ;

int main()
{
    int a[1000]={};
    int N , temp ;

    while(cin >>N)
    {
        for(int i=0; i<N; i++)
            cin >> a[i] ;
            cout<< endl ;
       

       for(int i=0; i<N-1; i++)
       {
           for(int j=0; j<N-1-i; j++)
          {
             if(a[j]>=a[j+1])
             {
                 temp=a[j+1] ;
                 a[j+1]=a[j] ;
                 a[j]=temp ;
              }
          }
       }
       for(int i=0; i<N; i++)

           cout<< a[i] << " ";
           cout<< endl ;
    }
return 0 ;
}

其實有sort

用法如下:

sort(a,a+N)