JAVA中sort函数的使用方法的个人总结
来源:互联网 发布:netflix ribbon源码 编辑:程序博客网 时间:2024/05/29 16:01
首先:sort函数的基本格式(默认排序为升序排序)
- Arrays.sort(数组名,起始下标,终止下标);
我们举个简单的例子
- 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;
- n=in.nextInt();
-
- for(int i=0;i<n;i++)
- {
- num[i]=in.nextInt();
-
- }
-
- Arrays.sort(num,0,n);
-
- for(int i=0;i<n;i++)
- {
- System.out.println(num[i]);
-
- }
- }
-
- }
- }
这个代码输入n个数,并把他们从小到大排序,运行结果如下。
大家注意一下,这里的起始下标和终止下标一定要是整形数。不能是浮点型。不然会报
用sort函数对浮点数呢?
- 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;
- n=in.nextInt();
-
- for(int i=0;i<n;i++)
- {
- num[i]=in.nextDouble();
-
- }
-
- Arrays.sort(num,0,n);
-
- for(int i=0;i<n;i++)
- {
- System.out.println(num[i]);
-
- }
- }
-
- }
- }
运行结果如下
如果一个数组初始化时已经赋值。则SORT函数可以另外一种格式
举个例子- 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]);
-
- }
-
-
- }
- }
然而,只是单纯的降序升序排序是满足不了使用需求得。
所以,我们要研究下sort函数中cmp函数的使用方法
我们来看看cmp函数的格式
- int compare(Object o1, Object o2);
我们可以看到,传入函数的是java中的类(java中没有结构体)这时,sort函数的格式变为
- Arrays.sort(数组名, 起始下标, 终止下标, new cmp());
怎么自定义排序呢?基本方法
int compare(Object o1, Object o2) 返回一个基本类型的整型
如果要按照升序排序,
则o1 小于o2,返回-1(负数),相等返回0,01大于02返回1(正数)
如果要按照降序排序
则o1 小于o2,返回1(正数),相等返回0,01大于02返回-1(负数)
以上就是在cmp函数中的自定义排序方法。
我们来举个例子。-----输入n个数,然后降序排序。
- import java.util.Arrays;
- import java.util.Comparator;
- import java.util.Scanner;
- import java.util.*;
-
- class shu
- {
- int x;
- }
-
- class cmp implements Comparator<shu> {
-
-
-
-
- public int compare(shu A, shu B)
- {
- 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];
-
- int n;
- n = in.nextInt();
-
- for (int i = 0; i < n; i++) {
-
- num[i]=new shu();
-
- 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);
- }
-
- }
- }
- }
运行结果
为了更好地理解,我们来举一些关于java结构体排序的题目
例一:HRBUST 1095 最麻烦了 点击打开链接
这题还有另外一种写法。(借用的)
- 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)
- {
-
- 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);
- }
- }
- }
- }
但是更多的题目中,我们会遇到字典序排序的情况
这里,我们再举一个例子
例2:HRBUST 1023 JiaoZhu and C 点击打开链接
这个代码提交会超时!!!!!!!!!!!!!