- Arrays.sort(数组名,起始下标,终止下标);
我们举个简单的例子
1. import java.util.*;
2. import java.util.Arrays;
3. public class Main {
4. public static void main(String[] args) {
5.
6. new Scanner(System.in);
7.
8. while(in.hasNext())
9. {
10. int num[]=new int[100];
11.
12. int n;///输出n个数
13. n=in.nextInt();
14.
15. for(int i=0;i<n;i++)
16. {
17. num[i]=in.nextInt();
18.
19. }
20.
21. 0,n);///排序部分
22.
23. for(int i=0;i<n;i++)
24. {
25. System.out.println(num[i]);
26.
27. }
28. }
29.
30. }
31. }
这个代码输入n个数,并把他们从小到大排序,运行结果如下。
大家注意一下,这里的起始下标和终止下标一定要是整形数。不能是浮点型。不然会报
用sort函数对浮点数呢?
1. import java.util.*;
2. import java.util.Arrays;
3. public class Main {
4. public static void main(String[] args) {
5.
6. new Scanner(System.in);
7.
8. while(in.hasNext())
9. {
10. double num[]=new double[100];
11.
12. int n;///输出n个数
13. n=in.nextInt();
14.
15. for(int i=0;i<n;i++)
16. {
17. num[i]=in.nextDouble();
18.
19. }
20.
21. 0,n);///排序部分
22.
23. for(int i=0;i<n;i++)
24. {
25. System.out.println(num[i]);
26.
27. }
28. }
29.
30. }
31. }
运行结果如下
如果一个数组初始化时已经赋值。则SORT函数可以另外一种格式
- Arrays.sort(数组名);
举个例子
1. import java.util.*;
2. import java.util.Arrays;
3. public class Main {
4. public static void main(String[] args) {
5.
6. new Scanner(System.in);
7.
8.
9. int num[]= {5,4,3,2,1};
10.
11. Arrays.sort(num);
12.
13. for(int i=0;i<5;i++)
14. {
15. System.out.println(num[i]);
16.
17. }
18.
19.
20. }
21. }
然而,只是单纯的降序升序排序是满足不了使用需求得。
所以,我们要研究下sort函数中cmp函数的使用方法
我们来看看cmp函数的格式
1. 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个数,然后降序排序。
1. import java.util.Arrays;
2. import java.util.Comparator;
3. import java.util.Scanner;
4. import java.util.*;
5.
6. class shu ///创建类
7. {
8. int x;
9. }
10.
11. class cmp implements Comparator<shu> {
12. /*
13. * 因为上面指定了类型<he>,所以此处可以直接(he A,he B) 否则要写成(Object A,Object
14. * B),再强制转换成he类型:((he)A).x
15. */
16. public int compare(shu A, shu B) ///降序排序
17. {
18. if(A.x<B.x)
19. {
20. return 1;
21. }
22. else if(A.x==B.x)
23. {
24. return 0;
25. }
26. else
27. {
28. return -1;
29. }
30.
31. }
32. }
33.
34. public class Main {
35. public static void main(String[] args) {
36.
37. new Scanner(System.in);
38.
39. while (in.hasNext()) {
40.
41. new shu[100];///创建类数组
42.
43. int n;
44. n = in.nextInt();
45.
46. for (int i = 0; i < n; i++) {
47.
48. new shu();///这个地方容易漏
49.
50. num[i].x = in.nextInt();
51. }
52.
53. 0, n, new cmp());
54.
55. for (int i = 0; i < n; i++) {
56. System.out.println(num[i].x);
57. }
58.
59. }
60. }
61. }
运行结果
为了更好地理解,我们来举一些关于java结构体排序的题目
例一:HRBUST 1095 最麻烦了 点击打开链接
1. import java.util.Arrays;
2. import java.util.Comparator;
3. import java.util.Scanner;
4. import java.util.*;
5.
6. class shu ///创建类
7. {
8. int acm;
9. int mon;
10. int rp;
11. }
12.
13. class cmp implements Comparator<shu> {
14. public int compare(shu A, shu B) ///降序排序
15. {
16. if(A.acm==B.acm)
17. {
18. if(A.mon==B.mon)
19. {
20. if(A.rp<B.rp)
21. {
22. return 1;
23. }
24. else if(A.rp==B.rp)
25. {
26. return 0;
27. }
28. else
29. {
30. return -1;
31. }
32. }
33. else
34. {
35. if(A.mon<B.mon)
36. {
37. return 1;
38. }
39. else if(A.mon==B.mon)
40. {
41. return 0;
42. }
43. else
44. {
45. return -1;
46. }
47. }
48. }
49. else
50. {
51. if(A.acm<B.acm)
52. {
53. return 1;
54. }
55. else if(A.acm==B.acm)
56. {
57. return 0;
58. }
59. else
60. {
61. return -1;
62. }
63. }
64.
65. }
66. }
67.
68. public class Main {
69. public static void main(String[] args) {
70.
71. new Scanner(System.in);
72.
73. while (in.hasNext()) {
74.
75. new shu[1005];///创建类数组
76.
77. int n;
78. n = in.nextInt();
79.
80. for (int i = 0; i < n; i++) {
81.
82. new shu();///这个地方容易漏
83.
84. num[i].acm = in.nextInt();
85. num[i].mon = in.nextInt();
86. num[i].rp = in.nextInt();
87. }
88.
89. 0, n, new cmp());
90.
91. for (int i = 0; i < n; i++) {
92. " "+num[i].mon+" "+num[i].rp);
93. }
94.
95. }
96. }
97. }
98. //int compare(Object o1, Object o2) 返回一个基本类型的整型
99. //如果要按照升序排序,
100. //则o1 小于o2,返回-1(负数),相等返回0,01大于02返回1(正数)
101. //如果要按照降序排序
102. // 则o1 小于o2,返回1(正数),相等返回0,01大于02返回-1(负数)
103. //
这题还有另外一种写法。(借用的)
1. import java.util.*;
2.
3. class mans
4. {
5. long acm,money,rp;
6. }
7. class cmp implements Comparator<mans>
8. {
9. public int compare(mans a,mans b)
10. {
11. if(a.acm==b.acm)
12. {
13. if(a.money==b.money)
14. {
15. return (int)(a.rp-b.rp);
16. }
17. return (int)(a.money-b.money);
18. }
19. return (int)(a.acm-b.acm);
20. }
21. }
22. public class Main
23. {
24.
25. public static void main(String[] args)
26. {
27. // TODO Auto-generated method stub
28. new Scanner(System.in);
29. new mans[1009];
30. while(scanf.hasNext())
31. {
32. int n=scanf.nextInt();
33. for(int i=0;i<n;i++)
34. {
35. new mans();
36. a[i].acm=scanf.nextLong();
37. a[i].money=scanf.nextLong();
38. a[i].rp=scanf.nextLong();
39. }
40. 0,n,new cmp());
41. for(int i=n-1;i>=0;i--)
42. {
43. " "+a[i].money+" "+a[i].rp);
44. }
45. }
46. }
47. }
但是更多的题目中,我们会遇到字典序排序的情况
这里,我们再举一个例子
例2:HRBUST 1023 JiaoZhu and C 点击打开链接
这个代码提交会超时!!!!!!!!!!!!!
1. import java.util.Arrays;
2. import java.util.Comparator;
3. import java.util.Scanner;
4. import java.util.*;
5.
6. class shu ///创建类
7. {
8. ///比较时用String
9. int mon;
10. int hunt;
11. }
12.
13. class cmp implements Comparator<shu> {
14. public int compare(shu A, shu B)
15. {
16. if(A.hunt==B.hunt)
17. {
18. if(A.mon==B.mon)
19. {
20. int flag=(A.name).compareTo(B.name);///按字典序排序
21. if(flag==0)
22. {
23. return 0;
24. }
25. else if(flag<0)
26. {
27. return -1;
28. }
29. else
30. {
31. return 1;
32. }
33. }
34. else
35. {
36. if(A.mon==B.mon)
37. {
38. return 0;
39.
40. }
41. else if(A.mon<B.mon)
42. {
43. return -1;
44. }
45. else
46. {
47. return 1;
48. }
49.
50. }
51. }
52. else
53. {
54.
55. if(A.hunt==B.hunt)
56. {
57. return 0;
58.
59. }
60. else if(A.hunt<B.hunt)
61. {
62. return 1;
63. }
64. else
65. {
66. return -1;
67. }
68. }
69.
70.
71. }
72.
73.
74. }
75.
76. public class Main {
77. public static void main(String[] args) {
78.
79. new Scanner(System.in);
80.
81. while (in.hasNext()) {
82.
83. new shu[100005];///创建类数组
84.
85. int n;
86. n = in.nextInt();
87.
88. for (int i = 0; i < n; i++) {
89.
90. new shu();///这个地方容易漏
91.
92. num[i].name=in.next();
93. num[i].hunt=in.nextInt();
94. num[i].mon=in.nextInt();
95. }
96.
97. 0, n, new cmp());
98.
99. for (int i = 0; i < n; i++) {
100. System.out.println(num[i].name);
101. }
102.
103. }
104. }
105. }