返回列表 發帖

[隨堂測驗] 排序 (三)

陣列 r[4]={-32,45,5,-67}
利用選擇排序法, 將陣列中的成員由大而小依序排列.
排列前: -32 45 5 -67
排列後: 45 5 -32 -67

本帖隱藏的內容需要回復才可以瀏覽

  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.     srand(time(NULL));
  8.     int n[]={-32,45,5,-67};
  9.     int tmp;
  10.     cout<<"排序前:";
  11.     for(int i=0;i<=3;i++)
  12.         cout<<n[i]<<" ";
  13.     for(int i=0;i<3;i++){

  14.         for(int j=i+1;j<3;j++){
  15.             if(n[i]<n[j]){
  16.                 n[i]=tmp;
  17.                 n[i]=n[j];
  18.                 n[j]=n[i];
  19.             }
  20.         }

  21.     }
  22.     cout<<endl<<"排序後:";
  23.     for(int i=0;i<4;i++)
  24.         cout<<n[i]<<" ";
  25.     system("pause");
  26.     return 0;
  27. }
複製代碼

TOP

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

TOP

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5. int main()
  6. {
  7.     int x;
  8.     srand(time(NULL));
  9.     int a[]{-32,45,5,-67};


  10.     cout<<"排序前: ";
  11.     for(int i=0; i<=3; i++)
  12.     {
  13.         cout<<a[i]<<" ";
  14.     }

  15.     for(int i=0; i<=3; i++)
  16.     {
  17.         for(int j=0; j<=3; j++)
  18.         {
  19.             if(a[i]>a[j])
  20.             {
  21.                 x=a[j];
  22.                 a[j]=a[i];
  23.                 a[i]=x;
  24.             }
  25.         }

  26.     }
  27.     cout<<endl<<"排序後: ";
  28.     for(int i=0; i<=3; i++)
  29.     {
  30.         cout<<a[i]<<" ";
  31.     }

  32. }
複製代碼

TOP

  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int t;
  7.     int a[]{-32,45,5,-67};
  8.     cout<<"排序前: ";
  9.     for(int i=0; i<=3; i++)
  10.     {
  11.         cout<<a[i]<<" ";
  12.     }
  13.     for(int i=0;i<=3;i++){
  14.         for(int j=i+1;j<=3;j++){
  15.             if(a[i]<a[j])
  16.             {
  17.                 t=a[j];
  18.                 a[j]=a[i];
  19.                 a[i]=t;
  20.             }

  21.         }


  22.     }

  23.     cout<<endl<<"排序後: ";
  24.     for(int i=0; i<=3; i++)
  25.     {
  26.         cout<<a[i]<<" ";
  27.     }

  28.     system("pause");

  29.     return 0;

  30. }
複製代碼

TOP

  1. #include <iostream>
  2. #include <cstdlib>
  3. using namespace std;
  4. int main()
  5. {
  6.     int x;
  7.     int a[4]={-32,45,5,-67};
  8.     cout<<"排序前: ";
  9.     for(int i=0; i<4; i++)
  10.     {
  11.         cout<<a[i]<<" ";
  12.     }

  13.     for(int i=0; i<4; i++)
  14.     {
  15.         for(int j=0; j<4; j++)
  16.         {
  17.             if(a[i]>a[j])
  18.             {
  19.                 x=a[j];
  20.                 a[j]=a[i];
  21.                 a[i]=x;
  22.             }
  23.         }

  24.     }
  25.     cout<<endl<<"排序後: ";
  26.     for(int i=0; i<4; i++)
  27.     {
  28.         cout<<a[i]<<" ";
  29.     }

  30. }
複製代碼

TOP

返回列表