• Java、泛型归并排序


     

    1. package sort;
    2. import java.security.SecureRandom;
    3. import java.util.Arrays;
    4. import java.util.Comparator;
    5. /**
    6. * @author: xyz
    7. * @create: 2022/8/14
    8. * @Description:
    9. * @FileName: Exercise23_02
    10. * @History:
    11. * @自定义内容:
    12. */
    13. public class Exercise23_02 {
    14. public static void main(String[] args) {
    15. Integer[] list1 = new Integer[10];
    16. for (int i = 0; i < list1.length; i++)
    17. list1[i] = new SecureRandom().nextInt(20);
    18. System.out.println(Arrays.toString(list1));
    19. mergeSort(list1, new SortComparator<>());
    20. System.out.println(Arrays.toString(list1));
    21. String[] list2 = {"China", "Babylon", "Paris", "america", "India", "Japan"};
    22. System.out.println(Arrays.toString(list2));
    23. mergeSort(list2, new SortComparator<>());
    24. System.out.println(Arrays.toString(list2));
    25. Double[] list3 = new Double[10];
    26. for (int i = 0; i < list3.length; i++)
    27. list3[i] = Math.random() * 20;
    28. System.out.println(Arrays.toString(list3));
    29. mergeSort(list3, new SortComparator<>());
    30. System.out.println(Arrays.toString(list3));
    31. }
    32. /** 使用Comparable接口的泛型归并排序 */
    33. public static extends Comparable> void mergeSort(E[] list) {
    34. if (list.length > 1) {
    35. E[] firstHalf = (E[])new Comparable[list.length / 2];
    36. System.arraycopy(list, 0, firstHalf, 0, list.length / 2);
    37. mergeSort(firstHalf);
    38. int secondHalfLength = list.length - list.length / 2;
    39. E[] secondHalf = (E[])new Comparable[secondHalfLength];
    40. System.arraycopy(list, list.length / 2, secondHalf, 0, secondHalfLength);
    41. mergeSort(secondHalf);
    42. merge(firstHalf, secondHalf, list);
    43. }
    44. }
    45. /** 使用Comparable接口的泛型归并 */
    46. private static extends Comparable> void merge(E[] list1, E[] list2, E[] temp) {
    47. int current1, current2, current3;
    48. current1 = current2 = current3 = 0;
    49. while (current1 < list1.length && current2 < list2.length)
    50. temp[current3++] = list1[current1].compareTo(list2[current2]) < 0 ? list1[current1++] : list2[current2++];
    51. while (current1 < list1.length)
    52. temp[current3++] = list1[current1++];
    53. while (current2 < list2.length)
    54. temp[current3++] = list2[current2++];
    55. }
    56. /** 使用Comparator接口的泛型归并排序 */
    57. public static void mergeSort(E[] list, Comparatorsuper E> comparator) {
    58. if (list.length > 1) {
    59. E[] firstHalf = (E[])new Object[list.length / 2];
    60. System.arraycopy(list, 0, firstHalf, 0, list.length / 2);
    61. mergeSort(firstHalf, comparator);
    62. int secondHalfLength = list.length - list.length / 2;
    63. E[] secondHalf = (E[])new Object[secondHalfLength];
    64. System.arraycopy(list, list.length / 2, secondHalf, 0, secondHalfLength);
    65. mergeSort(secondHalf, comparator);
    66. merge(firstHalf, secondHalf, list, comparator);
    67. }
    68. }
    69. /** 使用Comparator接口的泛型归并 */
    70. private static void merge(E[] list1, E[] list2, E[] temp, Comparatorsuper E> comparator) {
    71. int current1, current2, current3;
    72. current1 = current2 = current3 = 0;
    73. while (current1 < list1.length && current2 < list2.length)
    74. temp[current3++] = comparator.compare(list1[current1], list2[current2]) < 0 ? list1[current1++] : list2[current2++];
    75. while (current1 < list1.length)
    76. temp[current3++] = list1[current1++];
    77. while (current2 < list2.length)
    78. temp[current3++] = list2[current2++];
    79. }
    80. /** 静态内部类-泛型比较器类 */
    81. static class SortComparator implements Comparator {
    82. @Override
    83. public int compare(E o1, E o2) {
    84. if (o1 instanceof Comparable) {
    85. if (((Comparable) o1).compareTo(o2) > 0) return 1;
    86. else if (((Comparable) o1).compareTo(o2) < 0) return -1;
    87. else return 0;
    88. }
    89. //按哈希码排序
    90. if (o1.hashCode() > o2.hashCode())
    91. return 1;
    92. else if (o1.hashCode() < o2.hashCode())
    93. return -1;
    94. else
    95. return 0;
    96. }
    97. }
    98. }

     

  • 相关阅读:
    Redis(04)| 数据结构-压缩列表
    MySQL通过bin log日志恢复数据|手撕MySQL|对线面试官
    B站每日自动签到&传统单节点网站的 Serverless 上云
    向量数据库Milvus Cloud核心组件再升级,主打就是一个低延迟、高准确度
    纳科星融资逾2亿美元用于电池材料生产
    程序员购车指南
    springboot毕设项目宠物在线交易平台uu0m0(java+VUE+Mybatis+Maven+Mysql)
    【Java基础】ArrayList类概述、常用方法及存储字符串并遍历
    Unity 接口对接-时间戳
    计算机毕业设计ssm基于协同过滤推荐算法的新闻推荐系统43149系统+程序+源码+lw+远程部署
  • 原文地址:https://blog.csdn.net/m0_62659797/article/details/126375181