返回列表 發帖

排序 (一)

本帖最後由 陳品肇 於 2019-2-16 17:04 編輯

利用氣泡排序法, 將任意6個整數, 由小而大排列出來
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int tmp;
  7.     int n[6]={12,57, -6, -32, 0 ,23};
  8.     cout<<"排序前: ";
  9.     for(int i=0; i<6; i++)
  10.         cout<<n[i]<<" ";
  11.     for(int i=0; i<5; i++)
  12.     {
  13.         for(int j=i+1; j<6; j++) //我把陣列後的數值與當前去做比較
  14.         {
  15.             if(n[j]<n[i])
  16.             {
  17.                 tmp=n[i];
  18.                 n[i]=n[j];
  19.                 n[j]=tmp;
  20.             }
  21.         }
  22.     }
  23.     cout<<endl;
  24.     cout<<"排序後: ";
  25.     for(int i=0; i<6; i++)
  26.         cout<<n[i]<<" ";
  27.     cout<<endl;
  28.     system("pause");
  29.     return 0;   
  30. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int tmp;
  7.     int n[6]={12,57, -6, -32, 0 ,23};
  8.     cout<<"排序前: ";
  9.     for(int i=0; i<6; i++)
  10.         cout<<n[i]<<" ";
  11.     for(int i=0; i<5; i++)
  12.     {
  13.         for(int j=i+1; j<6; j++)  
  14.         {
  15.             if(n[j]<n[i])
  16.             {
  17.                 tmp=n[i];
  18.                 n[i]=n[j];
  19.                 n[j]=tmp;
  20.             }
  21.         }
  22.     }
  23.     cout<<endl;
  24.     cout<<"排序後: ";
  25.     for(int i=0; i<6; i++)
  26.         cout<<n[i]<<" ";
  27.     cout<<endl;
  28.     system("pause");
  29.     return 0;   
  30. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int tmp;
  7.     int n[6]={12,57, -6, -32, 0 ,23};
  8.      cout<<"排序前: ";
  9.     for(int i=0; i<6; i++)
  10.         cout<<n[i]<<" ";
  11.          for(int i=0; i<5; i++)
  12.     {
  13.         for(int j=i+1; j<6; j++) //我把陣列後的數值與當前去做比較
  14.         {
  15.             if(n[j]<n[i])
  16.             {
  17.                 tmp=n[i];
  18.                 n[i]=n[j];
  19.                 n[j]=tmp;
  20.             }
  21.         }
  22.     }
  23.     cout<<endl;
  24.     cout<<"排序後: ";
  25.     for(int i=0; i<6; i++)
  26.         cout<<n[i]<<" ";
  27.     cout<<endl;
  28.     system("pause");
  29.     return 0;   
  30. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()

  5. {
  6.     int tmp;
  7.     int n[6]={12,57, -6, -32, 0 ,23};
  8.     cout<<"排序前:";
  9.     for(int i=0;i<6;i++);
  10.       cout<<n[1]<<" ";
  11.       for(int i=0;i<5;i++)
  12.       {
  13.        for(int j=i+1;i<6;j++)      
  14.               {
  15.            if(n[j]>n[i])                        
  16.        {
  17.             tmp=n[i];
  18.             n[i]=n[j];
  19.             n[j]=tmp;
  20. }
  21.   }
  22.    }  
  23.       cout<<endl;
  24.      cout<<"排序後:";
  25.     for(int i=0; i<6; i++);
  26.     cout<<n[i]<<" ";
  27.    
  28.      cout<<endl;  
  29.      system("pause");
  30.      return 0;
  31.      
  32. }
複製代碼

TOP

本帖最後由 戴偉宸 於 2019-3-9 15:55 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int tmp;
  7.     int n[6]={12,57, -5, -82, 0 ,23};
  8.     cout<<"排序前: ";
  9.     for(int i=0; i<6; i++)
  10.         cout<<n[i]<<" ";
  11.     for(int i=0; i<5; i++)
  12.     {
  13.         for(int j=i+1; j<6; j++)
  14.         {
  15.             if(n[j]<n[i])
  16.             {
  17.                 tmp=n[i];
  18.                 n[i]=n[j];
  19.                 n[j]=tmp;
  20.             }
  21.         }
  22.     }
  23.     cout<<endl;
  24.     cout<<"排序後: ";
  25.     for(int i=0; i<6; i++)
  26.         cout<<n[i]<<" ";
  27.     cout<<endl;
  28.     system("pause");
  29.     return 0;   
  30. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int tmp;
  7.     int n[6]={-15, 25, -37, -99, 62};
  8.     cout<<"排序前: ";
  9.     for(int i=0;i<6;i++)
  10.        cout<<n[i]<<" ";
  11.     for(int i=0;i<5;i++)
  12.     {
  13.        for(int j=i+1;j<6;j++)
  14.        {
  15.           if(n[j]<n[i])
  16.           {
  17.              tmp=n[i];
  18.              n[i]=n[j];
  19.              n[j]=tmp;
  20.           }
  21.        }
  22.     }  
  23.     cout<<endl;
  24.     cout<<"排序後: ";
  25.     for(int i=0;i<6;i++)
  26.        cout<<n[i]<<" ";  
  27. system("pause");
  28. return 0;   
  29. }
複製代碼

TOP

本帖最後由 譚詩澐 於 2019-3-12 18:06 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.    int n[6]={12,57,-6,-32,0,23};
  7.    int tmp;
  8.    cout<<"排序前:";
  9.    for(int i=0;i<6;i++)
  10.    {
  11.          cout<<n[i]<<" ";  
  12.    }
  13.    for(int i=0;i<5;i++)
  14.    {
  15.       for(int j=i+1;j<6;j++)
  16.       {
  17.               if(n[j] < n[i])
  18.               {
  19.                       tmp = n[i];
  20.                       n[i] = n[j];
  21.                       n[j] = tmp;
  22.               }
  23.       }
  24.    }   
  25.     cout<<endl;
  26.     cout<<"排序後: ";
  27.     for(int i=0; i<6; i++)
  28.     cout<<n[i]<<" ";
  29.    cout<<endl;
  30.    system("pause");
  31.    return 0;   
  32. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int tim;
  7.     int x[6]={12,57,-6,-32,0,23};
  8.     cout<<"排序前:";
  9.     for(int i=0; i<6; i++)
  10.         cout<<x[i]<<" ";                        
  11.     cout<<endl;
  12.     for(int i=0; i<5; i++)
  13.     {
  14.         for(int j=i+1; j<6; j++)
  15.         {
  16.             if(x[j]<x[i])
  17.             {
  18.                 tim=x[i];
  19.                 x[i]=x[j];
  20.                 x[j]=tim;
  21.             }
  22.         }
  23.     }
  24.     cout<<"排序後:";
  25.     for(int i=0; i<6; i++)
  26.     cout<<x[i]<<" ";
  27.     cout<<endl;
  28.     system("pause");
  29.     return 0;
  30. }
複製代碼

TOP

返回列表