返回列表 發帖

[作業] 排序 (一)

本帖最後由 tonyh 於 2016-1-15 18:14 編輯

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

  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.         cout<<n[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(n[j]<n[i])
  17.              {
  18.                  tmp=n[j];
  19.                  n[j]=n[i];
  20.                  n[i]=tmp;         
  21.              }
  22.         }        
  23.     }
  24.     cout<<"排序後: ";
  25.     for(int i=0; i<6; i++)
  26.         cout<<n[i]<<" ";
  27.     cout<<endl;         
  28.     system("pause");
  29.     return 0;   
  30. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

本帖最後由 梁和雋 於 2015-9-23 21:09 編輯
  1. 26 -50 46 -41 16 產生變數

  2. -50<26  判斷小於或大於

  3. a=-50 r[j]=26 r[i]=-50 使項交換

  4. -50 26 46 -41 16 結果

  5. 46>-50  判斷小於或大於
  6. -41>-50  判斷小於或大於
  7. 16>-50  判斷小於或大於

  8. 46>26  判斷小於或大於
  9. -41<26  判斷小於或大於

  10. a=26 r[j]=-41 r[i]=26 使項交換

  11. -50 -41 46 26 16 結果

  12. 16>-41  判斷小於或大於

  13. 26<46  判斷小於或大於

  14. a=26 r[j]=46 r[i]=26 使項交換

  15. -50 -41 26 46 16 結果

  16. 16<26  判斷小於或大於

  17. a=16 r[j]=26 r[i]=16 使項交換

  18. -50 -41 16 46 26 結果

  19. 26<46  判斷小於或大於

  20. a=26 r[j]=46 r[i]=26 使項交換

  21. -50 -41 16 26 46 完成
複製代碼
這是手動排的,當筆記用
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     cout<<"亂數排列器"<<endl;
  7.     int g,p;
  8.     cout<<"每次要產生幾個變數"<<endl;
  9.     cin>>g;
  10.     int r[g];
  11.     cout<<"要產生幾組變數"<<endl;
  12.     cin>>p;
  13.     srand(time(NULL));
  14.     for(int q=1;q<=p;q++)
  15.     {
  16.     re:
  17.     for(int i=0; i<g; i++)
  18.     {
  19.          r[i]=rand()%100-50;
  20.          for(int j=0; j<i; j++)
  21.          {
  22.              if(r[j]==r[i])
  23.              {
  24.                  i--;
  25.                  break;              
  26.              }
  27.          }
  28.     }
  29.     int a;
  30.     cout<<"排序前: ";
  31.     for(int i=0; i<g; i++)
  32.         cout<<r[i]<<" ";
  33.     cout<<endl;
  34.     for(int i=0; i<g-1; i++)
  35.     {
  36.         for(int j=i+1; j<g; j++)
  37.         {
  38.              if(r[j]<r[i])
  39.              {
  40.                  a=r[j];
  41.                  r[j]=r[i];
  42.                  r[i]=a;
  43.                  for(int i=0; i<g; i++)
  44.                          cout<<r[i]<<" ";         
  45.              }
  46.              cout<<" ";
  47.         }
  48.         cout<<"移動第"<<i+1<<"項"<<endl;     
  49.     }
  50.     cout<<"排序後: ";
  51.     for(int i=0; i<g; i++)
  52.         cout<<r[i]<<" ";
  53.     cout<<endl<<endl;
  54.     }         
  55.     system("pause");
  56.     return 0;   
  57. }
複製代碼
這是電腦排出來的
http://fs-old.mis.kuas.edu.tw/~s1102137106/music/

TOP

本帖最後由 梁和雋 於 2015-9-26 13:29 編輯
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     cout<<"亂數排列器"<<endl;
  7.     bake:
  8.     int g,p;
  9.     cout<<"請輸入每次要產生的變數量"<<endl;
  10.     cin>>g;
  11.     int r[g];
  12.     cout<<"要產生幾組變數"<<endl;
  13.     cin>>p;
  14.     srand(time(NULL));
  15.     for(int q=1;q<=p;q++)
  16.     {
  17.     re:
  18.     for(int i=0; i<g; i++)
  19.     {
  20.          r[i]=rand()%100-50;
  21.          for(int j=0; j<i; j++)
  22.          {
  23.              if(r[j]==r[i])
  24.              {
  25.                  i--;
  26.                  break;              
  27.              }
  28.          }
  29.     }
  30.     int a;
  31.     cout<<"排序前: ";
  32.     for(int i=0; i<g; i++)
  33.         cout<<r[i]<<" ";
  34.     cout<<endl;
  35.     for(int i=0; i<g-1; i++)
  36.     {
  37.         for(int j=i+1; j<g; j++)
  38.         {
  39.              if(r[j]<r[i])
  40.              {
  41.                  a=r[j];
  42.                  r[j]=r[i];
  43.                  r[i]=a;         
  44.              }
  45.         }        
  46.     }
  47.     cout<<"排序後: ";
  48.     for(int i=0; i<g; i++)
  49.         cout<<r[i]<<" ";
  50.     cout<<endl<<endl;
  51.     }         
  52.     system("pause");
  53.     goto bake;
  54.     return 0;   
  55. }
複製代碼
http://fs-old.mis.kuas.edu.tw/~s1102137106/music/

TOP

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

TOP

返回列表