Java、泛型归并排序

 

package sort;

import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Comparator;

/**
 * @author: xyz
 * @create: 2022/8/14
 * @Description:
 * @FileName: Exercise23_02
 * @History:
 * @自定义内容:
 */
public class Exercise23_02 {
    public static void main(String[] args) {
        Integer[] list1 = new Integer[10];
        for (int i = 0; i < list1.length; i++)
            list1[i] = new SecureRandom().nextInt(20);

        System.out.println(Arrays.toString(list1));
        mergeSort(list1, new SortComparator<>());
        System.out.println(Arrays.toString(list1));

        String[] list2 = {"China", "Babylon", "Paris", "america", "India", "Japan"};
        System.out.println(Arrays.toString(list2));
        mergeSort(list2, new SortComparator<>());
        System.out.println(Arrays.toString(list2));

        Double[] list3 = new Double[10];
        for (int i = 0; i < list3.length; i++)
            list3[i] = Math.random() * 20;

        System.out.println(Arrays.toString(list3));
        mergeSort(list3, new SortComparator<>());
        System.out.println(Arrays.toString(list3));
    }

    /** 使用Comparable接口的泛型归并排序 */
    public static <E extends Comparable<E>> void mergeSort(E[] list) {
        if (list.length > 1) {
            E[] firstHalf = (E[])new Comparable[list.length / 2];
            System.arraycopy(list, 0, firstHalf, 0, list.length / 2);
            mergeSort(firstHalf);

            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, secondHalf, list);
        }
    }

    /** 使用Comparable接口的泛型归并 */
    private static <E extends Comparable<E>> void merge(E[] list1, E[] list2, E[] temp) {
        int current1, current2, current3;
        current1 = current2 = current3 = 0;

        while (current1 < list1.length && current2 < list2.length)
            temp[current3++] = list1[current1].compareTo(list2[current2]) < 0 ? list1[current1++] : list2[current2++];

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

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

    /** 使用Comparator接口的泛型归并排序 */
    public static <E> void mergeSort(E[] list, Comparator<? super E> comparator) {
        if (list.length > 1) {
            E[] firstHalf = (E[])new Object[list.length / 2];
            System.arraycopy(list, 0, firstHalf, 0, list.length / 2);
            mergeSort(firstHalf, comparator);

            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, secondHalf, list, comparator);
        }
    }

    /** 使用Comparator接口的泛型归并 */
    private static <E> void merge(E[] list1, E[] list2, E[] temp, Comparator<? super E> comparator) {
        int current1, current2, current3;
        current1 = current2 = current3 = 0;

        while (current1 < list1.length && current2 < list2.length)
            temp[current3++] = comparator.compare(list1[current1], list2[current2]) < 0 ? list1[current1++] : list2[current2++];

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

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

    /** 静态内部类-泛型比较器类 */
    static class SortComparator<E> implements Comparator<E> {
        @Override
        public int compare(E o1, E o2) {
            if (o1 instanceof Comparable) {
                if (((Comparable) o1).compareTo(o2) > 0) return 1;
                else if (((Comparable) o1).compareTo(o2) < 0) return -1;
                else return 0;
            }

            //按哈希码排序
            if (o1.hashCode() > o2.hashCode())
                return 1;
            else if (o1.hashCode() < o2.hashCode())
                return -1;
            else
                return 0;
        }
    }
}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值