泛型歸併排序(java 語言實現)

以下實例實現了

1. 實現了Comparable 接口的基本類型的泛型排序,

2. 複雜或者說自定義對象的泛型排序,使用 Comparator 接口定製的比較器

泛型歸併排序主要代碼

package com.shan.mergeSort;

import java.util.Comparator;

import org.junit.Test;


/*
 * 設計泛型方法要注意的事情:
 * 1 由於泛型只存在於編譯時,所以,不能使用 
 * new E(); 
 * new E[capacity]; 替代品是-->E[] elements = (E[]) new Object[capacity];
 * new ArrayList<E>[10] 也是不對的 --> 替代品 (ArrayList<String>[]) new ArrayList[10];
 * 由於泛型類的所有實例都有相同的運行時類,所以泛型類型的靜態變量和方法是被它的所有泛型類所共享的,
 * 所以,在靜態方法或者數據域,或者初始化語句中,爲了類而引用泛型參數是非法的。
 * 異常類不能是泛型,顯然。
 * 
 * 
 * */
public class GenericMergeSort {



    /** 
     * 
     *  1 Comparable :
     * 返回類型 之前的 <E extends Comparable<E>> 相當於是基於  mergeSort(E[] list) 中確定的 E ,
     * 更進一步的限定信息,一同用來給編譯器檢驗這個方法接受的參數是否符合 限定,
     * /
    /** The method for sorting the numbers */
    public static <E extends Comparable<E>> void mergeSort(E[] list) {
        if (list.length > 1) {
            // Merge sort the first half
            E[] firstHalf = (E[]) new Comparable[list.length / 2];
            System.arraycopy(list, 0, firstHalf, 0, list.length / 2);
            mergeSort(firstHalf);

            // Merge sort the second half
            int secondHalfLength = list.length - list.length / 2;

            E[] secondHalf = (E[]) new Comparable[secondHalfLength];
            System.arraycopy(list, list.length / 2, secondHalf, 0, secondHalfLength);
            mergeSort(secondHalf);

            // Merge firstHalf with secondHalf
            E[] temp = merge(firstHalf, secondHalf);
            System.arraycopy(temp, 0, list, 0, temp.length);
        }
    }

    private static <E extends Comparable<E>> E[] merge(E[] list1, E[] list2) {
        E[] temp = (E[]) new Comparable[list1.length + list2.length];

        int current1 = 0; // Index in list1
        int current2 = 0; // Index in list2
        int current3 = 0; // Index in temp

        while (current1 < list1.length && current2 < list2.length) {
            if (list1[current1].compareTo(list2[current2]) < 0) {
                temp[current3++] = list1[current1++];
            } else {
                temp[current3++] = list2[current2++];
            }
        }

        while (current1 < list1.length) {
            temp[current3++] = list1[current1++];
        }

        while (current2 < list2.length) {
            temp[current3++] = list2[current2++];
        }

        return temp;
    }


    /*
     * 
     * Comparator<? super E> 限定參數必須是 E 或E父類爲泛型類型所構造的比較器
     * */
    public static <E> void mergeSort(E[] list, Comparator<? super E> comparator) {
        if (list.length > 1) {
            // Merge sort the first half
            E[] firstHalf = (E[]) new Object[list.length / 2];
            System.arraycopy(list, 0, firstHalf, 0, list.length / 2);
            mergeSort(firstHalf, comparator);

            // Merge sort the second half
            int secondHalfLength = list.length - list.length / 2;
            E[] secondHalf = (E[]) new Object[secondHalfLength];
            System.arraycopy(list, list.length / 2, secondHalf, 0, secondHalfLength);
            mergeSort(secondHalf, comparator);

            // Merge firstHalf with secondHalf
            E[] temp = merge1(firstHalf, secondHalf, comparator);
            System.arraycopy(temp, 0, list, 0, temp.length);
        }
    }

    private static <E> E[] merge1(E[] list1, E[] list2, Comparator<? super E> comparator) {
        E[] temp = (E[]) new Object[list1.length + list2.length];

        int current1 = 0; // Index in list1
        int current2 = 0; // Index in list2
        int current3 = 0; // Index in temp

        while (current1 < list1.length && current2 < list2.length) {
            if (comparator.compare(list1[current1], list2[current2]) < 0) {
                temp[current3++] = list1[current1++];
            } else {
                temp[current3++] = list2[current2++];
            }
        }

        while (current1 < list1.length) {
            temp[current3++] = list1[current1++];
        }

        while (current2 < list2.length) {
            temp[current3++] = list2[current2++];
        }

        return temp;
    }



    /*
     * 適用對象: 基本類型的對象類型(包裝的),且實現了  Comparable interface
     * */
    @Test
    public void testMergeSort1(){

            Integer[] list = { 2, 3, 2, 5, 6, 1, -2, 3, 14, 12 };
            mergeSort(list);
            for (int i = 0; i < list.length; i++) {
                System.out.print(list[i] + " ");
            }

            System.out.println();

            Character[] list2 = {'a','A','W','K','d','a','w','c','b'};
            mergeSort(list2);
            for (int i = 0; i < list2.length; i++) {
                System.out.print(list2[i] + " ");
            }


    }


    /*
     * 
     * 適用比較複雜的定製對象:且要求定製一個比較器傳給排序的方法用來排序,比較器的泛型類型
     * GeometricObjectComparator implements Comparator<GeometricObject>, java.io.Serializable {
     * 一般是 需要比較的類的父類類型,或者 super interface,這樣才符合泛型程序設計理念。
     * */
    @Test 
    public void testMergeSort2(){
        Circle[] list1 = { new Circle(2), new Circle(3), new Circle(2), new Circle(5), new Circle(6), new Circle(1),
                new Circle(2), new Circle(3), new Circle(14), new Circle(12) };
        mergeSort(list1, new GeometricObjectComparator());
        for (int i = 0; i < list1.length; i++) {
            System.out.println(list1[i] + " ");
        }
    }



}

比較器代碼

package com.shan.mergeSort;

import java.util.Comparator;

/*
 * 定製比較器類 , implements Comparator<E>, java.io.Serializable {
 * */
public class GeometricObjectComparator implements Comparator<GeometricObject>, java.io.Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public int compare(GeometricObject o1, GeometricObject o2) {
        double area1 = o1.getArea();
        double area2 = o2.getArea();

        if (area1 < area2)
            return -1;
        else if (area1 == area2)
            return 0;
        else
            return 1;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章