返回列表 發帖

排序 (一)

本帖最後由 tonyh 於 2016-1-22 21:19 編輯

利用選擇排序法, 將任意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.     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. }
複製代碼

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

TOP

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

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.     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. }
複製代碼

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.     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. }
複製代碼

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.     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. }
複製代碼

TOP

返回列表