#输入整型数组和排序标识,元素按照升序或降序进行排序#
输入整型数组和排序标识,对其元素按照升序或降序进行排序
https://www.nowcoder.com/practice/dd0c6b26c9e541f5b935047ff4156309
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int n = in.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = in.nextInt(); } int flag = in.nextInt(); quickSort(arr, 0, arr.length - 1); // Arrays.sort(arr); if (flag == 0) { for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } if (flag == 1) { for (int i = arr.length -1; i >= 0; i--) { System.out.print(arr[i] + " "); } } } } private static void quickSort(int[] arr, int l, int r) { if (l < r) { int point = getPoint(arr, l, r); quickSort(arr, l, point - 1); quickSort(arr, point + 1, r); } } private static int getPoint(int[] arr, int l, int r) { int point = arr[l]; int i = l + 1; int j = r; while (i <= j) { if (arr[i] <= point) { i++; } else { swap(arr, i, j); j--; } } //point交换 swap(arr, l, j); return j; } //写快速排序算法对数组进行排序 private static void swap(int[] arr, int i, int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }