Sorting issues Time limit:1000ms Memory limit:65536k have questions? Dot here ^_^ The title description enters 10 integers, outputs them from small to large, and gives the position of each element in the original sequence. Input input data has a row, containing 10 integers, separated by a space. The output output data has two lines, the first behavior is sorted after the sequence, and the second behaves after the position of each element in the original sequence. Sample input
1 2 3 5 4 6 8 9 10 7
Sample output
1 2 3 4 5 6 7 8 9 101 2 3 5 4 6 10 7 8 9
The sort provided by the arrays class can only be sorted in ascending order, and is a monotone number or letter, but can be modified by the comparator comparer to sort the class, I thought about it. Perhaps this enables a descending sort of a single numeric array? Let's try.
Import java.io.*;import java.util.*;import java.math.*;import java.text.*;class node{public int tag,num;} Class CMP implements Comparator<node>{public int Compare (node A,node B) {return a.num-b.num;}} public class Main {public static void main (string[] args) {Scanner in=new Scanner (system.in); node[] a=new node[11];for (int i=0;i<10;i++) {a[i]=new node (); A[i].num=in.nextint (); a[i].tag=i+1;} Arrays.sort (a,0,10,new cmp ()); for (int i=0;i<10;i++) if (i!=9) System.out.print (a[i].num+ ""); elseSystem.out.print (A[i].num); System.out.println (""); for (int i=0;i<10;i++) if (i!=9) System.out.print (a[i].tag+ ""); ElseSystem.out.print (A[i]. TAG); System.out.println ("");}}
Sdut 1196-Sorting problems (Java class sort)