左神演算法基礎班 第3章 第二週學習——詳解桶排序以及排序內容大總結
阿新 • • 發佈:2020-12-22
3.1 比較器
- 返回負數的時候,第一個引數排在前面
- 返回正數的時候,第二個引數排在前面
- 返回0的時候,誰在前面無所謂(預設排序)
定義 Student 類,以年齡 age 逆序輸出。寫了兩種不同的比較器格式,習慣用第一種。
import java.util.*; public class comparatorDemo { public static class Student { public String name; public int id; public int age; public Student (String name, int id, int age) { this.name = name; this.id = id; this.age = age; } } public static void main (String[] args) { Student std1 = new Student("摸魚人生", 1, 16); Student std2 = new Student("多快樂", 2, 22); Student std3 = new Student("大海的對面", 3, 18); Student std4 = new Student("沒有自由", 4, 25); Student[] stdArr = new Student[] {std1, std2, std3, std4}; for (int i = 0; i < stdArr.length; i++) { System.out.println(stdArr[i].name + " " + stdArr[i].id + " " + stdArr[i].age); } System.out.println("============"); Arrays.sort(stdArr, new Comparator<Student>() { // 返回負數的時候,第一個引數排在前面 public int compare (Student std1, Student std2) { // 返回正數的時候,第二個引數排在前面 return std2.age - std1.age; // 返回0的時候,誰在前面無所謂(預設排序) } }); for (int i = 0; i < stdArr.length; i++) { System.out.println(stdArr[i].name + " " + stdArr[i].id + " " + stdArr[i].age); } } }
import java.util.*; public class comparatorDemo { public static class Student { public String name; public int id; public int age; public Student (String name, int id, int age) { this.name = name; this.id = id; this.age = age; } } public static void main (String[] args) { Student std1 = new Student("摸魚人生", 1, 16); Student std2 = new Student("多快樂", 2, 22); Student std3 = new Student("大海的對面", 3, 18); Student std4 = new Student("沒有自由", 4, 25); Student[] stdArr = new Student[] {std1, std2, std3, std4}; for (int i = 0; i < stdArr.length; i++) { System.out.println(stdArr[i].name + " " + stdArr[i].id + " " + stdArr[i].age); } System.out.println("============"); Arrays.sort(stdArr, new studentComp()); for (int i = 0; i < stdArr.length; i++) { System.out.println(stdArr[i].name + " " + stdArr[i].id + " " + stdArr[i].age); } } public static class studentComp implements Comparator<Student> { public int compare (Student std1, Student std2) { return std1.age - std2.age; } } }
第三種方式是以大根堆的結構彈出。
import java.util.*; public class comparatorDemo { public static class Student { public String name; public int id; public int age; public Student (String name, int id, int age) { this.name = name; this.id = id; this.age = age; } } public static void main (String[] args) { Student std1 = new Student("摸魚人生", 1, 16); Student std2 = new Student("多快樂", 2, 22); Student std3 = new Student("大海的對面", 3, 18); Student std4 = new Student("沒有自由", 4, 25); Student[] stdArr = new Student[] {std1, std2, std3, std4}; for (int i = 0; i < stdArr.length; i++) { System.out.println(stdArr[i].name + " " + stdArr[i].id + " " + stdArr[i].age); } System.out.println("============"); // 以大根堆的結構彈出 PriorityQueue<Student> heap = new PriorityQueue<> (new studentComp()); heap.add(std1); heap.add(std2); heap.add(std3); heap.add(std4); while (!heap.isEmpty()) { Student curStd = heap.poll(); System.out.println(curStd.name + " " + curStd.id + " " + curStd.age); } } public static class studentComp implements Comparator<Student> { public int compare (Student std1, Student std2) { return std2.age - std1.age; } } }