描述
输入整型数组和排序标识,对其元素按照升序或降序进行排序
数据范围: 1 \le n \le 1000 \1≤n≤1000 ,元素大小满足 0 \le val \le 100000 \0≤val≤100000
输入描述:
第一行输入数组元素个数
第二行输入待排序的数组,每个数用空格隔开
第三行输入一个整数0或1。0代表升序排序,1代表降序排序
输出描述:
输出排好序的数字

import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int a = in.nextInt();
int[] list = new int[a];
for (int i = 0; i < a; i++) {
list[i] = in.nextInt();
}
int b = in.nextInt();
Arrays.sort(list);
if (b == 0) {
for (int j = 0; j < a; j++) {
System.out.print(list[j] + " ");
}
System.out.println();
}
else{
for (int j = a-1; j >= 0; j--) {
System.out.print(list[j] + " ");
}
System.out.println();
}
}
}
}
该博客展示了一个使用Java实现的排序算法,通过Scanner读取整型数组和排序方式(升序或降序)。程序首先获取数组长度,然后输入数组元素,接着根据输入的标志进行排序。如果标志为0,使用Arrays.sort进行升序排序并打印;若为1,则从后向前遍历数组进行降序排序并输出。这个例子适合初学者理解数组排序的基本操作。

1454

被折叠的 条评论
为什么被折叠?



