备战蓝桥杯java(十三): 算法竞赛中的常用API:sort方法和自定义比较器的写法

摘要

在做一些算法题时常常会需要对数组、自定义对象、集合进行排序. 在java中对数组排序提供了Arrays.sort()方法,对集合排序提供Collections.sort()方法。对自定义对象排序时要自己重写比较器,对象数组则调用Arrays.sort(),对象集合则调用Collections.sort()。两个方法默认都是升序,也可以重写比较器,实现降序。

对数组排序

sort函数模板, 以int型数组为例:

Arrays.sort(int[] a, Comparator<Integer>() {
	public int compare(int a, int b){
		 return a - b;   升序
	
		// return b - a;   降序
		/* a-b>0 交换ab位置,反之不变, 即返回值为正数时,交换数组中正在比较
		的两个元素的位置,返回值为负数时,不交换。 记住第一个参数去比较第二个
		参数就是升序,第二个参数比较第一个参数就是降序就OK了。
		*/
	}
})

例题: 快速排序

给定你一个长度为n的整数数列。

请你使用快速排序对这个数列按照从小到大进行排序。

并将排好序的数列按顺序输出。

输入格式
输入共两行,第一行包含整数 n。

第二行包含 n 个整数(所有整数均在1~109范围内),表示整个数列。

输出格式
输出共一行,包含 n 个整数,表示排好序的数列。

数据范围
1≤n≤100000
输入样例:
5
3 1 2 4 5
输出样例:
1 2 3 4 5

可以将字符串数组转化为整型数组之后在排序,为了演示自定义比较器的写法这里直接对字符串数组进行排序

import java.io.*;
import java.util.*;

public class Main{
	// 输入输出模板
	static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
	
	static int n;
	public static void main(String[] args) throws IOException {
		n = Integer.parseInt(in.readLine());
		String s[] = in.readLine().split(" ");// 读入数据
		
		Arrays.sort(s, new Comparator<String>() { // 排序
			public int compare(String a, String b) {  
	        	if(a.length() == b.length()){ // 如果长度相等则直接比较字典序
	        		return a.compareTo(b);
	        	}
	        	else{ // 长度长的一定大
	        		return a.length() - b.length();
	        	}
			}
		});
		
		for(String p : s){
			out.write(p+" ");
		}
		
		out.flush();
	}
}

对集合进行排序

创建TreeSet实例,对其从大到小排序。
因为TreeSet是自动排序和去重的, 默认为升序,我们可以重写比较器构造一个降序的TreeSet, 之后添加数据就会自动排序。

import java.io.*;
import java.util.*;

public class Main{
	static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
	
	public static void main(String[] args) throws IOException {
		
		TreeSet<Integer> s = new TreeSet<>(new Comparator<Integer>(){
			public int compare(Integer a, Integer b) {  
	        	return b - a; 
			}
		});
		
		s.add(10);
		s.add(5);
		s.add(4);
		s.add(6);
		s.add(7);
		s.add(8);
		s.add(1);
		s.add(2);
		s.add(3);
		s.add(9);
		
		for(Integer p : s){
			out.write(p+" ");
		}
		out.flush();
	}
}
输出:
10 9 8 7 6 5 4 3 2 1

对自定义对象数组排序

创建学生类, 按照年龄从小到大排序

import java.io.*;
import java.util.*;

class student{ 
	int age;
	String name;
	student(int a, String b){
		this.age = a;
		this.name = b;
	}
}
public class Main{
	static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
	static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
	
	public static void main(String[] args) throws IOException {
		student s[] = new student[4]; //创建学生对象数组
		s[0] = new student(10, "1");
		s[1] = new student(9, "2");
		s[2] = new student(8, "3");
		s[3] = new student(7, "4");
		
		Arrays.sort(s, new Comparator<student>() {
			public int compare(student a, student b) {  //
	        	return a.age - b.age; // 按照年龄大小升序排列
			}
		});
		for(student p : s){
			out.write(p.age+" "+p.name + "\n");
		}
		out.flush();
	}
}

输出:
7 4
8 3
9 2
10 1

大致就是这样了,还可以对要排序的类实现Comparable接口,重写compareTo方法,但是稍稍有些麻烦,本文不再介绍。

发布了66 篇原创文章 · 获赞 208 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/GD_ONE/article/details/104146301
今日推荐