鱼C论坛

 找回密码
 立即注册
查看: 1236|回复: 1

[已解决]bubble sort 程式問題

[复制链接]
发表于 2022-4-7 21:34:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
想請問各位如何把程式排序的過程都顯示出來
還有如何讓他橫向排列,我的是直的

#include <iostream>
using namespace std;

int main()
{
    int x[5]={18,2,20,34,12};
    int i,j;
   
    for(i=0;i<5;i++){
        cout << x[i] << endl;
    }
    for(i=0;i<4;i++){
        for(j=0;j<4-i;j++){
            if(x[j]>x[j+1]){
                int x1 = x[j];
                x[j] = x[j+1];
                x[j+1] = x1;
            }
        }
    }
    for(i=0;i<5;i++){
        cout << x[i] << endl;
    }
    return 0;
}

感謝各位
最佳答案
2022-4-7 23:06:28
  1. #include <iostream>
  2. using namespace std;

  3. int main()
  4. {
  5.     int x[5]={18,2,20,34,12};
  6.     int i,j;
  7.    
  8.     for(i=0;i<5;i++){
  9.         cout << x[i] << " ";
  10.     }
  11.         cout << endl << endl;

  12.     for(i=0;i<4;i++){
  13.         for(j=0;j<4-i;j++){
  14.             if(x[j]>x[j+1]){
  15.                                 cout << "交换:" << x[j] << "  " << x[j+1] << endl;
  16.                 int x1 = x[j];
  17.                 x[j] = x[j+1];
  18.                 x[j+1] = x1;
  19.                                 continue;
  20.             }
  21.                         cout << "保持不变:" << x[j] << endl;
  22.         }

  23.                 cout << "交换后为:" ;
  24.                 for(int k=0; k<5; k++)
  25.                 {
  26.                         cout << x[k] << " ";
  27.                 }
  28.                 cout << endl;
  29.                 cout << "-------------第"<< i+1 <<"轮交换结束---------------"  << endl;               
  30.                
  31.     }
  32.         cout << endl << endl;

  33.     for(i=0;i<5;i++){
  34.         cout << x[i] << " ";
  35.     }
  36.         cout << endl << endl;


  37.     return 0;
  38. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-4-7 23:06:28 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. using namespace std;

  3. int main()
  4. {
  5.     int x[5]={18,2,20,34,12};
  6.     int i,j;
  7.    
  8.     for(i=0;i<5;i++){
  9.         cout << x[i] << " ";
  10.     }
  11.         cout << endl << endl;

  12.     for(i=0;i<4;i++){
  13.         for(j=0;j<4-i;j++){
  14.             if(x[j]>x[j+1]){
  15.                                 cout << "交换:" << x[j] << "  " << x[j+1] << endl;
  16.                 int x1 = x[j];
  17.                 x[j] = x[j+1];
  18.                 x[j+1] = x1;
  19.                                 continue;
  20.             }
  21.                         cout << "保持不变:" << x[j] << endl;
  22.         }

  23.                 cout << "交换后为:" ;
  24.                 for(int k=0; k<5; k++)
  25.                 {
  26.                         cout << x[k] << " ";
  27.                 }
  28.                 cout << endl;
  29.                 cout << "-------------第"<< i+1 <<"轮交换结束---------------"  << endl;               
  30.                
  31.     }
  32.         cout << endl << endl;

  33.     for(i=0;i<5;i++){
  34.         cout << x[i] << " ";
  35.     }
  36.         cout << endl << endl;


  37.     return 0;
  38. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-25 02:29

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表