범용 병합 정렬 (자바 언어 구현)

아래 의 실례 가 실현 되 었 다
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;
    }
}

좋은 웹페이지 즐겨찾기