输入整型数组和排序标识,对其元素按照升序或降序进行排序
数据范围: ,元素大小满足
第一行输入数组元素个数第二行输入待排序的数组,每个数用空格隔开第三行输入一个整数0或1。0代表升序排序,1代表降序排序
输出排好序的数字
8 1 2 4 9 3 55 64 25 0
1 2 3 4 9 25 55 64
5 1 2 3 4 5 1
5 4 3 2 1
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); List<Integer> r = new ArrayList(); for (int i = 0; i < n; ++ i) { r.add(in.nextInt()); } Collections.sort(r); if (in.nextInt() == 1) Collections.reverse(r); r.forEach(item -> { System.out.printf("%d ", item);}); } }
import java.util.*; import java.util.stream.Collectors; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int numSize = in.nextInt(); List<Integer> nums = new ArrayList<>(); for (int i = 0; i < numSize; i++) { nums.add(in.nextInt()); } List<Integer> list = nums.stream().sorted().collect(Collectors.toList()); int lastInt = in.nextInt(); if (lastInt == 0) { for (int i = 0; i < list.size(); i++) { System.out.print(list.get(i) + " "); } } else { for (int i = list.size() - 1; i >= 0; i--) { System.out.print(list.get(i) + " "); } } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int k = in.nextInt(); Integer[] nums = new Integer[k]; for (int i = 0; i < k; i++) { nums[i] = in.nextInt(); } if (in.nextInt() == 0) { Arrays.sort(nums); } else { Arrays.sort(nums, (a, b)->(b-a)); } for (int num : nums) { System.out.print(num + " "); } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); List<Integer> list = new ArrayList(); for(int i = 0; i < n; i++){ list.add(in.nextInt()); } int t = in.nextInt(); Collections.sort(list, (o1, o2)-> t == 0 ? o1 - o2: o2 - o1); list.forEach(k -> System.out.print(k + " ")); } }
import java.util.ArrayList; import java.util.Comparator; import java.util.Iterator; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s1 = sc.nextLine(); ArrayList<Integer> list = new ArrayList<>(); String arrstr = sc.nextLine(); String[] split = arrstr.split(" "); for (int i = 0; i < split.length; i++) { int num = Integer.parseInt(split[i]); list.add(num); } String sss = sc.nextLine(); int i = Integer.parseInt(sss); if (i == 0) { sx(list); bl(list); } else { jx(list); bl(list); } } //升序方法 public static void sx(ArrayList<Integer> list) { list.sort(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o1 - o2; } }); } //降序方法 public static void jx(ArrayList<Integer> list) { list.sort(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return o2 - o1; } }); } //遍历list public static void bl(ArrayList<Integer> list) { Iterator<Integer> it = list.iterator(); while (it.hasNext()) { Integer next = it.next(); System.out.print(next + " "); } } }
import java.util.Arrays; import java.util.List; import java.util.Scanner; import java.util.TreeSet; import java.util.stream.Collectors; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int totalNum = sc.nextInt(); int[] source = new int[totalNum]; for (int i = 0; i < totalNum; i++) { source[i] = sc.nextInt(); } boolean orderFlag = 0 == sc.nextInt(); TreeSet<Integer> integers = new TreeSet<>((pre, current) -> { int diff = pre - current; diff = orderFlag ? diff : -diff; return diff == 0 ? 1 : diff; }); for (int i : source) { integers.add(i); } StringBuilder result = new StringBuilder(); for (Integer integer : integers) { result.append(" ").append(integer); } System.out.println(result.delete(0, 1)); } }
import java.util.Scanner; import java.util.Arrays; import java.util.Collections; // 注意类名必须为 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 a = in.nextInt(); int[] array = new int[a]; for (int i = 0; i < a; i++) { array[i] = in.nextInt(); } int b = in.nextInt(); if (b == 0) { Arrays.sort(array); for (int y = 0; y < a; y++) { System.out.print(array[y] + " "); } } else { for (int u = 0; u < a; u++) { for (int i = 0; i < a - 1; i++) { if (array[i] < array[i + 1]) { int num = array[i + 1]; array[i + 1] = array[i]; array[i] = num; } } } for (int y = 0; y < a; y++) { System.out.print(array[y] + " "); } } } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int a = in.nextInt(); Integer[] arr = new Integer[a]; for(int i = 0;i< a ;i++){ int c = in.nextInt(); arr[i] = c; } int b = in.nextInt(); if(b == 0){ Arrays.sort(arr); }else{ Arrays.sort(arr,Collections.reverseOrder()); } for(int i : arr){ System.out.print(i+" "); } } }
简单粗暴
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); while (input.hasNext()) { int length = input.nextInt(); int[] datas = new int[length]; for (int i = 0; i < length; i++) { datas[i] = input.nextInt(); } int order = input.nextInt(); arrayOrder(length, order, datas); } } private static void arrayOrder(int length, int order, int[] datas) { List<Integer> arrayList = new ArrayList<>(length); for (int data : datas) { arrayList.add(data); } if (order == 0) { // 升序 Collections.sort(arrayList); } else { // 降序 arrayList.sort((element1, element2) -> element1 > element2 ? -1 : 1 ); } arrayList.forEach(element -> System.out.print(element + " ")); System.out.println(); } }
public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int totalNum = in.nextInt(); ArrayList<Integer> arrayList = new ArrayList<>(); for (int i = 0; i < totalNum; i++) arrayList.add(in.nextInt()); int sort = in.nextInt(); Collections.sort(arrayList); if (sort == 0) { for (Integer integer : arrayList) System.out.print(integer + " "); } else for (int i = arrayList.size() - 1; i >= 0; i--) System.out.print(arrayList.get(i) + " "); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 int num = in.nextInt(); int i=0; int [] arr = new int[num]; while (i<num){ arr[i] = in.nextInt(); i++; } int flag = in.nextInt(); mergeSort(arr,0,num-1,flag); for (int i1 : arr) { System.out.print(i1+" "); } } public static void mergeSort(int[] arr,int first,int end,int flag){ if(first>=end){ return; } int mid = first+(end-first)/2; mergeSort(arr,first,mid,flag); mergeSort(arr,mid+1,end,flag); int[] tmp = new int[end-first+1]; int i =0, length=end-first+1, j=first, k=mid+1; if(flag==0){ //升序 while (i<length){ if(j>=first && j<=mid){ if(k>mid && k<=end){ if(arr[j]<arr[k]){ tmp[i]=arr[j]; j++; }else { tmp[i]=arr[k]; k++; } }else { tmp[i]=arr[j]; j++; } }else { tmp[i]=arr[k]; k++; } i++; } }else{ //降序 while (i<length){ if((j>=first && j<=mid)){ if((k>mid && k<=end)){ if(arr[j]>arr[k]){ tmp[i]=arr[j]; j++; }else { tmp[i]=arr[k]; k++; } }else { tmp[i]=arr[j]; j++; } }else { tmp[i]=arr[k]; k++; } i++; } } for(i=0;i<length;i++){ arr[first+i] = tmp[i]; } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int n = in.nextInt(); Integer[] numbers = new Integer[n]; for(int i=0; i<n; i++){ numbers[i] = in.nextInt(); } int flag = in.nextInt(); if(flag==0) Arrays.sort(numbers); else Arrays.sort(numbers, (a, b) -> b-a); for(int i : numbers){ System.out.print(i + " "); } } }