比较器接口(Comparator interface):用可选顺序(alternate order)进行排序

public interface Comparator<key>
  int compare(Key v, Key w) //比较元素v和w

示例:

Java系统排序(java system sort)

  • 创建Comparator对象(Create Comparator object)
  • 向Array.sort()传递第二参数
String[] a;
...
Arrays.sort(); //自然顺序(natural order)
...
Arrays.sort(a, String.CASE_INSENSITIVE_ORDER);
Arrays.sort(a, Collator.getInstance(new Locale("es"));
Arrays.sort(a, new BritishPhoneBookOrder()); //这三个顺序参数都是被Comparato<String>对象所定义的

使用Comparator接口进行选择排序(Insertion sort)

public static void sort(Object[] a, Comparator comparator)
{
int N = a.length;
for (int i = ; i < N;i++)
for(int j = i; j > && less(comparator, a[j], a[j-]); j--)
exch(a, j, j-);
} private static boolean less(Comparator c, Object v, Object w)
{ return c.compare(v, w) < ; } private static void exch(Object[] a, int i, int j)
{ Object swap = a[i]; a[i] = a[j]; a[j] = swap; }

实现Comparator接口

  • 定义实现 Comparator 接口的类
  • 实现 compare() 方法
public class Student
{
public static final Comparator<Student> BY_NAME = new ByName();
public static final Comparator<Student> BY_SECTION = new BySection();
private final String name;
private final int section;
...
private static class ByName implements Comparator<Student> //ByName类实现了Comparator接口
{
public int compare(Student v, Student w)
{ return v.name.compareTo(w.name); }
} private static class BySection implements Comparator<Student> //BySection实现了Comparator接口
{
public int compare(Student v, Student w)
{ return v.section - w.section; }
}
}

真正的比较在compare的重载方法中。

Comparator接口是一层壳,是把compare()方法包装起来的包装纸。

compareTo()方法是compare()的具体实现

普林斯顿大学算法课 Algorithm Part I Week 3 比较器 Comparators的更多相关文章

  1. 普林斯顿大学算法课 Algorithm Part I Week 3 排序算法复杂度 Sorting Complexity

    计算复杂度(Computational complexity):用于研究解决特定问题X的算法效率的框架 计算模型(Model of computation):可允许的操作(Allowable oper ...

  2. 普林斯顿大学算法课 Algorithm Part I Week 3 快速排序 Quicksort

    发明者:Sir Charles Antony Richard Hoare 基本思想: 先对数据进行洗牌(Shuffle the array) 以数据a[j]为中心进行分区(Partition),使得a ...

  3. 普林斯顿大学算法课 Algorithm Part I Week 3 归并排序 Mergesort

    起源:冯·诺依曼最早在EDVAC上实现 基本思想: 将数组一分为(Divide array into two halves) 对每部分进行递归式地排序(Recursively sort each ha ...

  4. 普林斯顿大学算法课 Algorithm Part I Week 3 排序的应用 System Sorts

    排序算法有着广泛的应用 典型的应用: 排序名称 排序MP3音乐文件 显示Google的网页排名的搜索结果 按标题顺序列出RSS订阅 排序之后下列问题就变得非常简单了 找出中位数(median) 找出统 ...

  5. 普林斯顿大学算法课 Algorithm Part I Week 3 重复元素排序 - 三路快排 Duplicate Keys

    很多时候排序是为了对数据进行归类,这种排序重复值特别多 通过年龄统计人口 删除邮件列表里的重复邮件 通过大学对求职者进行排序 若使用普通的快排对重复数据进行排序,会造成N^2复杂度,但是归并排序和三路 ...

  6. 普林斯顿大学算法课 Algorithm Part I Week 3 求第K大数 Selection

    问题 给定N个元素的数组,求第k大的数. 特例当k=0时,就是求最大值,当k=N-1时,就是求最小值. 应用顺序统计求top N排行榜 基本思想 使用快速排序方法中的分区思想,使得a[k]左侧没有更小 ...

  7. 普林斯顿大学算法课 Algorithm Part I Week 3 排序稳定性 Stability

    稳定性(Stability):先按性质A排序,再按性质B排序,性质B相同的那些项是否仍然是按性质A排序的? 一个稳定的排序,相同值的元素应仍保持相对顺序(relative order) 稳定的算法:插 ...

  8. 普林斯顿大学算法课 Algorithm Part I Week 3 自我总结

    要熟练掌握比较器Comparator public final Comparator<T> MY_COMPARATOR = new myComparator(); //定义比较器 .... ...

  9. 普林斯顿大学算法课 Algorithm Part I 学习资源

    网友笔记参考 果壳Mooc首页 revilwang的专栏 白色咖啡 Weiran Liu的渣技术小专栏 Bug表:http://findbugs.sourceforge.net/bugDescript ...

随机推荐

  1. humble number(hd1058)

    Problem Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The ...

  2. iOS中判断设备系统版本

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  3. Asp.net MVC 之 ActionResult

    Action运行完后,回传的值通过ActionResult 类别或者其衍生的类别操作.ActionResult是一个抽象类,因此,Asp.net MVC 本身就实作了许多不同类型的ActionResu ...

  4. Azure上如何在Linux下挂载数据磁盘

    [原文首次发表于51cto http://cloudapps.blog.51cto.com/3136598/1653672] 在Azure上创建了虚拟机之后,我们在一些情况下会需要添加更多的数据磁盘来 ...

  5. 環氧樹脂對COB的影響

    COB的封膠一般使用單液 Epoxy(環氧樹脂),也可以使用雙液(Epoxy+硬化劑),雙液的成品品質可靠度雖然比較高,但是保存及維護非常麻煩,所以一般的 COB 都還是採用單液的製程. 其次是如何控 ...

  6. uva 10245 The Closest Pair Problem_枚举

    题意:求任意两点之间的距离的最少一个距离 思路:枚举一下就可以了 #include <iostream> #include<cstdio> #include<cmath& ...

  7. poj 1157 LITTLE SHOP_简单dp

    题意:给你n种花,m个盆,花盆是有顺序的,每种花只能插一个花盘i,下一种花的只能插i<j的花盘,现在给出价值,求最大价值 简单dp #include <iostream> #incl ...

  8. jQuery下实现检测指定元素加载完毕

    检测元素出现方法.虽然是基于 jQuery 的,但是代码很简洁,可以修改成纯js版的. 文本 jQuery.fn.wait = function (func, times, interval) { v ...

  9. 编写javascript的基本技巧

    第一.编写可维护的代码 什么叫着编写可维护的代码呢?就是当我的做出来的项目,拿给其它编码团队能很快的看懂 你编写的代码,你的整个项目的逻辑等等.一个项目的修改维护是要比开发一个项目的成本 是要高的.例 ...

  10. ASP.NET MVC 学习之路-6

    本文在于巩固基础 上文中使用的Code First创建数据库 本文将使用数据库生成模型 这里使用ADO.NET实体数据模型来生成模型 下面按照指导完成操作 下面看看如何使用这个框架 数据访问修改主要是 ...