返回列表 發帖

排序 (一)

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

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int tmp;
  7.     int n[]={12,57,-6,-32,0,23};
  8.    
  9.     cout<<"排序前: ";
  10.     for(int i=0; i<6; i++)
  11.     {
  12.         cout<<n[i]<<" ";
  13.     }
  14.         cout<<endl;
  15.        
  16.     //開始排序
  17.     for(int i=0; i<5; i++)
  18.     {
  19.         for(int j=i+1; j<6; j++)
  20.         {
  21.             //倆倆(i,j)比較
  22.             //右邊小於左邊就交換->換到最後最左邊會最小
  23.             if(n[j]<n[i])
  24.             {
  25.                 //兩數交換
  26.                 tmp=n[j];
  27.                 n[j]=n[i];
  28.                 n[i]=tmp;
  29.             }
  30.         }   
  31.     }
  32.    
  33.     cout<<"排序後: ";
  34.     for(int i=0; i<6; i++)
  35.     {
  36.             cout<<n[i]<<" ";
  37.     }
  38.         cout<<endl;
  39.        
  40.     system("pause");
  41.     return 0;
  42. }
複製代碼

9

TOP

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

TOP

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

TOP

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

TOP

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

TOP

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

TOP

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

TOP

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

TOP

返回列表