冒泡排序高逼格
Posted 鹏鹏进阶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了冒泡排序高逼格相关的知识,希望对你有一定的参考价值。
import java.util.Comparator; /** * 排序器接口(策略模式: 将算法封装到具有共同接口的独立的类中使得它们可以相互替换) * * @author Chengpeng * */ interface Sorter { /** * 排序 * * @param list * 待排序的数组 */ public <T extends Comparable<T>> void sort(T[] list); /** * 排序 * * @param list * 待排序的数组 * @param comp * 比较两个对象的比较器 */ public <T> void sort(T[] list, Comparator<T> comp); } /** * 冒泡排序 * * * */ public class BubbleSorter implements Sorter { @Override public <T extends Comparable<T>> void sort(T[] list) { boolean swapped = true; for (int i = 1, len = list.length; i < len && swapped; ++i) { swapped = false; for (int j = 0; j < len - i; ++j) { if (list[j].compareTo(list[j + 1]) > 0) { T temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; swapped = true; } } } } @Override public <T> void sort(T[] list, Comparator<T> comp) { boolean swapped = true; for (int i = 1, len = list.length; i < len && swapped; ++i) { swapped = false; for (int j = 0; j < len - i; ++j) { if (comp.compare(list[j], list[j + 1]) > 0) { T temp = list[j]; list[j] = list[j + 1]; list[j + 1] = temp; swapped = true; } } } } public static void main(String[] args) { Person p1 = new Person(10); Person p2 = new Person(20); Person p3 = new Person(80); Person p4 = new Person(40); Person[] list = { p1, p2, p3, p4 }; // new BubbleSorter().sort(list, new PersonComparator()); //or new BubbleSorter().sort(list, new Comparator<Person>() { @Override public int compare(Person o1, Person o2) { return o1.score - o2.score; } }); for (int i = 0; i < list.length; i++) { System.out.println(list[i]); } } } class Person { int score; public Person(int score) { this.score = score; } public String toString() { return String.valueOf(this.score); } } class PersonComparator implements Comparator<Person> { @Override public int compare(Person o1, Person o2) { return o2.score - o1.score; } }
以上是关于冒泡排序高逼格的主要内容,如果未能解决你的问题,请参考以下文章