我编写的程序按升序对数组进行排序,将其排序为降序,我不明白为什么。任何帮助赞赏。数组排序按降序而不是升序排列
#include<iostream>
using namespace std;
int main() {
int a[20] = { 21, 31, 1, 5, 3, 7, 87, 324, 21, 990, 34, 33, 21, 123, 54, 45,
66, 76, 23, 44 }, t;
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
if (a[i] < a[j]) {
t = a[i];
a[i] = a[j];
a[j] = t;
}
}
}
cout << endl;
for (int i = 0; i < 20; i++) {
cout << a[i] << "\t";
}
}
改变你的内循环;或者正确读取数组以及如何使用内部或外部循环来比较值。 –
它没有,当我测试它。 – molbdnilo