我得到了一个将 int[] a 从低到高排序的算法。
public static void sortering(int[] a){
int temp;
for(int i = 0; i < a.length; i++){
for(int j = i + 1; j < a.length; j++){
if(a[i] > a[j]){
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
}
我想做的就是反转它,让它从高到低排序。我以为这就像在公园里散步一样,做这样的事情:
public static void sorteringU(int[] a){
int temp;
for(int i = a.length; i < a.length; i--){
for(int j = i - 1; j < a.length; j--){
if(a[i] > a[j]){
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
}
我错了,这显然没有任何作用。 有谁愿意帮忙吗?
编辑:谢谢 Jesper 和 Satya,它成功了。
最佳答案
这就足够了:
public static void sorteringU(int[] a){
int temp;
for(int i = 0; i < a.length; i++){
for(int j = i + 1; j < a.length; j++){
if(a[i] < a[j]){ // Change ">" to "<"
temp = a[j];
a[j] = a[i];
a[i] = temp;
}
}
}
}
关于java - 反转排序算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19630635/