实现一个快速排序 将接口对象作为类属性
//: Compare.java // Interface for sorting callback: package c08; interface Compare { boolean lessThan(Object lhs, Object rhs); boolean lessThanOrEqual(Object lhs, Object rhs); } // /:~
排序的实现:
//: SortVector.java // A generic sorting vector package c08; import java.util.*; public class SortVector extends Vector { private Compare compare; // To hold the callback public SortVector(Compare comp) { compare = comp; } public void sort() { quickSort(0, size() - 1); } // 4 5 private void quickSort(int left, int right) { if (right > left) { Object o1 = elementAt(right); int i = left - 1; //3 int j = right; //5 while (true) { while (compare.lessThan(elementAt(++i), o1)) ; while (j > 0) if (compare.lessThanOrEqual(elementAt(--j), o1)) break; // out of while if (i >= j) break; swap(i, j); } swap(i, right); quickSort(left, i - 1); quickSort(i + 1, right); } } /** * 数据交换位置 * @param loc1 * @param loc2 */ private void swap(int loc1, int loc2) { Object tmp = elementAt(loc1); //获取loc1位置的数据 setElementAt(elementAt(loc2), loc1); //将loc2位置的数据放置在loc1上 setElementAt(tmp, loc2); //将原loc1上的数据放在loc2上 } } // /:~
测试案例(需要实现排序的具体规则)
//: StringSortTest.java // Testing the generic sorting Vector package c08; import java.util.*; public class StringSortTest { static class StringCompare implements Compare { public boolean lessThan(Object l, Object r) { return ((String) l).toLowerCase().compareTo( ((String) r).toLowerCase()) < 0; } public boolean lessThanOrEqual(Object l, Object r) { return ((String) l).toLowerCase().compareTo( ((String) r).toLowerCase()) <= 0; } } public static void main(String[] args) { SortVector sv = new SortVector(new StringCompare()); sv.addElement("d"); sv.addElement("A"); sv.addElement("C"); sv.addElement("c"); sv.addElement("b"); sv.addElement("B"); sv.addElement("D"); sv.addElement("a"); sv.sort(); Enumeration e = sv.elements(); while (e.hasMoreElements()) System.out.println(e.nextElement()); } } // /:~
可以通过匿名内部类来实现:
//: StrSortVector.java // Automatically sorted Vector that // accepts and produces only Strings package c08; import java.util.*; public class StrSortVector { private SortVector v = new SortVector( // Anonymous inner class: new Compare() { public boolean lessThan(Object l, Object r) { return ((String) l).toLowerCase().compareTo( ((String) r).toLowerCase()) < 0; } public boolean lessThanOrEqual(Object l, Object r) { return ((String) l).toLowerCase().compareTo( ((String) r).toLowerCase()) <= 0; } }); private boolean sorted = false; //增加新的元素以后需要重新排序 public void addElement(String s) { v.addElement(s); sorted = false; } public String elementAt(int index) { if (!sorted) { v.sort(); sorted = true; } return (String) v.elementAt(index); } public Enumeration elements() { if (!sorted) { v.sort(); sorted = true; } return v.elements(); } // Test it: public static void main(String[] args) { StrSortVector sv = new StrSortVector(); sv.addElement("d"); sv.addElement("A"); sv.addElement("C"); sv.addElement("c"); sv.addElement("b"); sv.addElement("B"); sv.addElement("D"); sv.addElement("a"); Enumeration e = sv.elements(); while (e.hasMoreElements()) System.out.println(e.nextElement()); } } // /:~