【发布时间】:2021-01-05 05:22:03
【问题描述】:
当我以降序输入(如4321)时它工作正常,但当我更改顺序时失败,即使是像1432 这样的一个位置
for(int pass = 1; pass <= n-1; pass++)
{
for(int j = 0; j < n-pass; j++)
{
if( a[j] > a[j+1])
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
输出
Enter the size of the array
4
Enter the elements of the array
1
4
3
2
Elements of the array are :
1432
Elements of the sorted array are :
0001
【问题讨论】:
-
你缺少括号
-
现在是学习的好时机:总是使用括号。即使是单行块。
-
@OP C++ 不是 Python。
-
非常感谢您的帮助,我是初学者,我会学习
-
你违反了 if 语句的基本结构,因为如果我们必须在 if 语句中执行多个语句,它需要括号!
标签: c++ bubble-sort