冒泡排序降序java
时间: 2023-12-26 15:28:48 浏览: 66
以下是使用Java实现冒泡排序降序的代码示例:
```java
import java.util.Scanner;
public class BubbleSort {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] a = new int[5];
System.out.println("请输入五个数字");
for (int i = 0; i < 5; i++) {
a[i] = sc.nextInt();
}
for (int i = 0; i < 5; i++) {
for (int j = 1; j < 5 - i; j++) {
if (a[j - 1] < a[j]) { // 降序排序,改为小于号
int b = a[j - 1];
a[j - 1] = a[j];
a[j] = b;
}
}
}
for (int i = 0; i < 5; i++) {
System.out.print(a[i] + " ");
}
}
}
```
阅读全文