泛型归并排序(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;
    }
}

你可能感兴趣的:(java,泛型,泛型归并排序)