When it comes to sorting, the first thing that comes to mind is the sort function. But, the sort function in java is a little different from the sort function in c++.
Here, I will roughly list the use of the sort function.
First: the basic format of the sort function (the default sort is ascending sort)
Arrays.sort(array name, start index, end index);
Let's take a simple example
import java.util. *; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); while(in.hasNext()) { int num[] = new int[100]; int n;///output n numbers n=in.nextInt(); for(int i=0;i<n;i++) { num[i]=in.nextInt(); } Arrays.sort(num,0,n);///sort part for(int i=0;i<n;i++) { System.out.println(num[i]); } } } }
This code inputs n numbers and sorts them from small to large. The results are as follows.
Please note that the starting subscript and ending subscript here must be integer numbers. Cannot be a float. Otherwise, it will be reported
What about floating point numbers with the sort function?
import java.util. *; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); while(in.hasNext()) { double num[]=new double[100]; int n;///output n numbers n=in.nextInt(); for(int i=0;i<n;i++) { num[i]=in.nextDouble(); } Arrays.sort(num,0,n);///sort part for(int i=0;i<n;i++) { System.out.println(num[i]); } } } }
The running result is as follows
If an array is initialized with a value already assigned . Then the SORT function can be in another format
Arrays.sort(array name);
for example
import java.util. *; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); int num[]= {5,4,3,2,1}; Arrays.sort(num); for(int i=0;i<5;i++) { System.out.println(num[i]); } } }
However, simply sorting in descending and ascending order cannot meet the needs of use.
Therefore, we have to study the use of the cmp function in the sort function
Let's take a look at the format of the cmp function
int compare(Object o1, Object o2);
We can see that what is passed into the function is a class in java (there is no structure in java)
At this time, the format of the sort function becomes
Arrays.sort(array name, start index, end index, new cmp());
How to customize sorting?
basic method
int compare(Object o1, Object o2) Returns an integer of primitive type.
If
o1 is less than o2, return -1 (negative number), if 01 is greater than 02, return 1 (positive number)
if 01 is greater than 02 In descending order
, if o1 is less than o2, return 1 (positive number), return 0 if equal, return -1 (negative number) if 01 is greater than 02
Let's take an example. -----Enter n numbers, and then sort in descending order.
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; import java.util. *; class shu ///Create a class { int x; } class cmp implements Comparator<shu> { /* * Because the type <he> is specified above, it can be directly (he A, he B) here, otherwise it should be written as (Object A, Object * B), then cast to he type: ((he)A).x */ public int compare(shu A, shu B) ///sort in descending order { if(A.x<B.x) { return 1; } else if(A.x==B.x) { return 0; } else { return -1; } } } public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { shu num[] = new shu[100];///Create a class array int n; n = in.nextInt(); for (int i = 0; i < n; i++) { num[i]=new shu();///This place is easy to leak num[i].x = in.nextInt(); } Arrays.sort(num, 0, n, new cmp()); for (int i = 0; i < n; i++) { System.out.println(num[i].x); } } } }
operation result
For a better understanding, let's take some questions about the sorting of java structures
Example 1: HRBUST 1095 is the most troublesome, click to open the link
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; import java.util. *; class shu ///Create a class { int acm int my; int rp; } class cmp implements Comparator<shu> { public int compare(shu A, shu B) ///sort in descending order { if(A.acm==B.acm) { if(A.mon==B.mon) { if(A.rp<B.rp) { return 1; } else if(A.rp==B.rp) { return 0; } else { return -1; } } else { if(A.mon<B.mon) { return 1; } else if(A.mon==B.mon) { return 0; } else { return -1; } } } else { if(A.acm<B.acm) { return 1; } else if(A.acm==B.acm) { return 0; } else { return -1; } } } } public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { shu num[] = new shu[1005];///Create a class array int n; n = in.nextInt(); for (int i = 0; i < n; i++) { num[i]=new shu();///This place is easy to leak num[i].acm = in.nextInt(); num[i].mon = in.nextInt(); num[i].rp = in.nextInt(); } Arrays.sort(num, 0, n, new cmp()); for (int i = 0; i < n; i++) { System.out.println(num[i].acm+" "+num[i].mon+" "+num[i].rp); } } } } //int compare(Object o1, Object o2) returns a primitive integer //If you want to sort in ascending order, //Then o1 is less than o2, return -1 (negative number), return 0 if equal, return 1 (positive number) if 01 is greater than 02 //If you want to sort in descending order // If o1 is less than o2, return 1 (positive number), return 0 if equal, return -1 (negative number) if 01 is greater than 02 //There is another way of writing this question. (borrowed)
import java.util. *; class mans { long acm,money,rp; } class cmp implements Comparator<mans> { public int compare(mans a,mans b) { if(a.acm==b.acm) { if(a.money==b.money) { return (int)(a.rp-b.rp); } return (int)(a.money-b.money); } return (int)(a.acm-b.acm); } } public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanf=new Scanner(System.in); mans a[]=new mans[1009]; while(scanf.hasNext()) { int n=scanf.nextInt(); for(int i=0;i<n;i++) { a[i] = new mans(); a[i].acm=scanf.nextLong(); a[i].money=scanf.nextLong(); a[i].rp=scanf.nextLong(); } Arrays.sort(a,0,n,new cmp()); for(int i=n-1;i>=0;i--) { System.out.println(a[i].acm+" "+a[i].money+" "+a[i].rp); } } } }
But in more topics, we will encounter lexicographical sorting
Here, we give another example
Example 2: HRBUST 1023 JiaoZhu and C Click to open the link
This code submission will time out! ! ! ! ! ! ! ! ! ! ! ! !
import java.util.Arrays; import java.util.Comparator; import java.util.Scanner; import java.util. *; class shu ///Create a class { String name;///Use String when comparing int my; int hunt; } class cmp implements Comparator<shu> { public int compare(shu A, shu B) { if(A.hunt==B.hunt) { if(A.mon==B.mon) { int flag=(A.name).compareTo(B.name);///Sort lexicographically if(flag==0) { return 0; } else if(flag<0) { return -1; } else { return 1; } } else { if(A.mon==B.mon) { return 0; } else if(A.mon<B.mon) { return -1; } else { return 1; } } } else { if(A.hunt==B.hunt) { return 0; } else if(A.hunt<B.hunt) { return 1; } else { return -1; } } } } public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { shu num[] = new shu[100005];///Create a class array int n; n = in.nextInt(); for (int i = 0; i < n; i++) { num[i]=new shu();///This place is easy to leak num[i].name=in.next(); num[i].hunt=in.nextInt(); num[i].mon=in.nextInt(); } Arrays.sort(num, 0, n, new cmp()); for (int i = 0; i < n; i++) { System.out.println(num[i].name); } } } }
It's about purple