返回列表 發帖

排序 (二) 複習氣泡排序

利用氣泡排序法, 將任意6個整數, 由小而大排列出來.

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

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int tmp;
  7.     int num[6]={0,9,10,5,8,4};
  8.     cout<<"排序前: ";
  9.     for(int i=0; i<6; i++)
  10.         cout<<num[i]<<" ";
  11.     for(int i=0; i<5; i++)
  12.     {
  13.         for(int j=i+1; j<6; j++)
  14.         {
  15.             if(num[j]<num[i])
  16.             {
  17.                 tmp=num[i];
  18.                 num[i]=num[j];
  19.                 num[j]=tmp;
  20.             }
  21.         }
  22.     }
  23.     cout<<endl;
  24.     cout<<"排序後: ";
  25.     for(int i=0; i<6; i++)
  26.         cout<<num[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 seq[6]={96,-55,901,-16,374,-2380};
  7.     int tmp;
  8.     cout<<"原數列: ";
  9.     for(int i=0; i<6; i++)
  10.     {
  11.         cout<<seq[i]<<" ";
  12.     }
  13.     for(int i=0; i<5; i++)
  14.     {
  15.         for(int j=i+1; j<6; j++)
  16.         {
  17.             if(seq[j]<seq[i])
  18.             {
  19.                 tmp=seq[i];
  20.                 seq[i]=seq[j];
  21.                 seq[j]=tmp;
  22.             }
  23.         }
  24.     }
  25.     cout<<endl;
  26.     cout<<"排序後: ";
  27.     for(int i=0; i<6; i++)
  28.     {
  29.         cout<<seq[i]<<" ";
  30.     }
  31.     cout<<endl;
  32.     system("pause");
  33.     return 0;
  34. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int aa;
  7.     int n[6]={3,2,56,99,56,10};
  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.                 aa=n[i];
  18.                 n[i]=n[j];
  19.                 n[j]=aa;
  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=0;
  7. int n[6]={12,57,-6,-32,0,23};  
  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[j];
  20.         n[j]=n[i];
  21.         n[i]=tmp;
  22.        }
  23.       }
  24.      }
  25.       cout<<"排序後";
  26.       for(int i=0; i<6; i++)
  27.       {
  28.       cout<<n[i]<<" ";
  29.       }
  30.       cout<<endl;
  31.       system("pause");
  32.       return 0;   
  33. }
複製代碼

TOP

本帖最後由 黃安立 於 2018-8-27 10:33 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6. int aa;
  7. int n[6]={3,2,56,99,56,10};
  8. cout<<前";
  9. for(int a=0;a<6;a++)
  10. cout<<n[a]<<" ";
  11.   for(int a=0; a<5; a++)
  12.     {
  13.         for(int z=a+1; z<6; z++)
  14.         {
  15.                 if(n[z]<n[a])
  16.             {
  17.                 aa=n[a];
  18.                 n[a]=n[z];
  19.                 n[z]=aa;
  20.       }
  21.      }
  22.     }
  23.     cout<<endl;
  24.     cout<<"後:";
  25.     for(int a=0; a<6; a++)
  26.     cout<<n[a]<<" ";
  27.     cout<<endl;
  28.     system("pause");
  29.     return 0;   
  30. }
複製代碼

TOP

返回列表