<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    聶永的博客

    記錄工作/學習的點點滴滴。

    模板模式以及java算法的復習

    針對常用的算法進行重新復習了一下,結合對象數組排序,使用模板模式封裝了一部分,各個子類實現自己的對象數組排序算法。里面稍微涉及到了適配器、策略模式等。使用了泛型,嗯,會帶來一部分代碼的復用。
    定義一個算法抽象基類:
    /**
    * 對象數組排序算法,藉此復習幾種常見排序算法
    *
    * 模板模式
    *
    **/

    public abstract class Sort {

    /**
    * 對象數組排序
    * @param <T>
    * @param ts 傳入要排序的對象數組
    * @param c 需要傳入的自定義比較器
    */

    public abstract <T> void sort(T[] ts, Comparator<? super T> c);

    /**
    * 對象數組排序
    * @param <T>
    * @param datas 傳入的對象必須已經實現了Comparable接口
    */

    public <T extends Comparable<T>> void sort(T[] datas) {
    sort(datas, new ComparatorAdapter<T>());
    }

    /**
    * 定義一個比較適配器內部類,使Comparable接口適配成Comparator接口
    *
    * @author xiaomin
    *
    * @param <T>
    */

    private class ComparatorAdapter<T extends Comparable<T>> implements
    Comparator<T> {
    public int compare(T o1, T o2) {
    return o1.compareTo(o2);
    }
    }

    /**
    * 交換對象數組里面的兩個元素位置
    * @param <T>
    * @param ints
    * @param index1
    * @param index2
    */

    protected <T> void swap(T[] ints, int index1, int index2) {
    T temp = ints[index1];

    ints[index1] = ints[index2];

    ints[index2] = temp;
    }
    }
    一個冒泡算法實現:
    /**
    * 冒泡排序----交換排序的一種
    * 方法:相鄰兩元素進行比較,如有需要則進行交換,每完成一次循環就將最大元素排在最后(如從小到大排序),下一次循環是將其他的數進行類似操作。
    * 性能:比較次數O(n^2),n^2/2;交換次數O(n^2),n^2/4
    *
    **/

    public class Bubble extends Sort{

    @Override
    public <T> void sort(T[] datas, Comparator<? super T> c){
    for(int i = (datas.length - 1); i > 0; i--){
    for(int j = 0; j < i; j++){
    if(c.compare(datas[j], datas[j + 1]) > 0){
    super.swap(datas, j, j+1);
    }
    }
    }
    }

    public static void main(String ... args){
    Bubble b = new Bubble();
    Integer [] ints = {1, 23, 0, 2, 356, 367, 6,-1, 256};
    b.sort(ints);

    System.out.println(Arrays.toString(ints));
    }
    }
    一個插入排序:
    /**
    * 插入排序
    * 方法:將一個記錄插入到已排好序的有序表(有可能是空表)中,從而得到一個新的記錄數增1的有序表。
    * 性能:比較次數O(n^2),n^2/4
    * 復制次數O(n),n^2/4
    * 比較次數是前兩者的一般,而復制所需的CPU時間較交換少,所以性能上比冒泡排序提高一倍多,而比選擇排序也要快。
    *
    */

    public class Insert extends Sort{

    @Override
    public <T> void sort(T[] datas, Comparator<? super T> c){
    for(int i = 0; i < datas.length; i++){
    for(int j = i; j > 0; j--){
    if(c.compare(datas[j -1],datas[j]) > 0){
    super.swap(datas, j-1, j);
    }
    }
    }
    }

    public static void main(String [] args){
    Insert insert = new Insert();
    Double [] doubles = {1.0, 0.3, 3.4, -0.3, 3.0,3.0, 0.34};

    insert.sort(doubles);

    System.out.println(Arrays.toString(doubles));
    }
    }
    其它兩個,快速,選擇排序不再一一粘貼。
    附加一個客戶端測試類:
    public class Client {

    public static void main(String[] args) {
    Person[] persons = { new Person("a", 12), new Person("b", 10),
    new Person("demo", 23), new Person("hello", 22),
    new Person("hello", 32), new Person("xiaomeng", 2) };

    Person [] selectPersons = persons.clone();

    Person [] quickPersons = persons.clone();

    Person [] quickCustomPersons = persons.clone();

    Person [] insertPersons = persons.clone();

    System.out.println("排序前 ......");
    System.out.println(Arrays.toString(persons));
    System.out.println();

    System.out.println("冒泡排序后 ......");
    new Bubble().sort(persons);
    System.out.println(Arrays.toString(persons));
    System.out.println();

    System.out.println("選擇排序后 ......");
    new Select().sort(selectPersons);
    System.out.println(Arrays.toString(selectPersons));
    System.out.println();

    System.out.println("插入排序后 ......");
    new Insert().sort(insertPersons);
    System.out.println(Arrays.toString(insertPersons));
    System.out.println();

    System.out.println("快速排序后 ......");
    new Quick().sort(quickPersons);
    System.out.println(Arrays.toString(quickPersons));
    System.out.println();

    System.out.println("使用快速自定義排序 ......");
    new Quick().sort(quickCustomPersons, new Comparator<Person>() {
    public int compare(Person o1, Person o2) {// 倒敘排列
    return o1.getAge() > o2.getAge() ? -1 : (o1.getAge() == o2.getAge() ? 0 : 1);
    }
    });

    System.out.println(Arrays.toString(quickCustomPersons));
    }
    }

    class Person implements Serializable, Comparable<Person> {
    private static final long serialVersionUID = -23536L;

    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
    this.name = name;
    this.age = age;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public int getAge() {
    return age;
    }

    public void setAge(int age) {
    this.age = age;
    }

    // 正序排列
    public int compareTo(Person o) {
    if (o == null)
    return -1;

    return this.getAge() < o.getAge() ? -1 : (this.getAge() == o.getAge() ? 0 : 1);
    }

    public String toString() {
    return "name : " + getName() + " age : " + getAge();
    }
    }

    代碼打包下載地址: 下載




    posted on 2010-10-06 10:59 nieyong 閱讀(391) 評論(0)  編輯  收藏 所屬分類: Java

    公告

    所有文章皆為原創,若轉載請標明出處,謝謝~

    新浪微博,歡迎關注:

    導航

    <2010年10月>
    262728293012
    3456789
    10111213141516
    17181920212223
    24252627282930
    31123456

    統計

    常用鏈接

    留言簿(58)

    隨筆分類(130)

    隨筆檔案(151)

    個人收藏

    最新隨筆

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲乱码中文论理电影| 国产精品免费观看调教网| 久久久久久久91精品免费观看| 成年女人毛片免费观看97| 亚洲国产精品日韩在线观看| 色欲aⅴ亚洲情无码AV| 国产VA免费精品高清在线| 亚洲AV无码之日韩精品| 久久亚洲美女精品国产精品| 亚洲av无一区二区三区| 成人黄18免费视频| 亚洲精品二区国产综合野狼| 中文字幕免费在线视频| 成人免费视频一区| 亚洲色www永久网站| 国产精品免费小视频| 免费看一级一级人妻片| 亚洲一区爱区精品无码| 97公开免费视频| 亚洲成av人片在线天堂无| 亚洲AV无码一区二区三区国产 | 我要看WWW免费看插插视频| 亚洲午夜无码久久久久软件| 国产又长又粗又爽免费视频| 一级毛片免费在线观看网站| 亚洲成人动漫在线| 性生交片免费无码看人| 亚洲最大福利视频网站| 成年女人午夜毛片免费视频| 一级毛片a免费播放王色电影 | 久久不见久久见免费影院www日本| 性色av无码免费一区二区三区| 亚洲AV无码久久精品狠狠爱浪潮| WWW亚洲色大成网络.COM| 成人免费视频77777| 日韩在线观看免费完整版视频| 成人免费无码精品国产电影| 日本中文字幕免费看| 亚洲电影一区二区| 日韩精品免费视频| 亚洲heyzo专区无码综合|