• 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_03
    10. * @History:
    11. * @自定义内容:
    12. */
    13. public class Exercise23_03 {
    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. quickSort(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. quickSort(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. quickSort(list3, new SortComparator<>());
    30. System.out.println(Arrays.toString(list3));
    31. }
    32. /** 使用Comparable接口的泛型快速排序 */
    33. public static extends Comparable> void quickSort(E[] list) {
    34. quickSort(list, 0, list.length - 1);
    35. }
    36. /** 使用Comparable接口的泛型快速排序辅助方法 */
    37. private static extends Comparable> void quickSort(E[] list, int low, int high) {
    38. if (low > high) return;
    39. E temp = list[low];
    40. int i = low;
    41. int j = high;
    42. while (i != j) {
    43. while (i < j && (list[j].compareTo(temp) > 0 || list[j].compareTo(temp) == 0)) j--;
    44. while (i < j && (list[i].compareTo(temp) < 0 || list[i].compareTo(temp) == 0)) i++;
    45. if (i < j) {
    46. E t = list[i];
    47. list[i] = list[j];
    48. list[j] = t;
    49. }
    50. }
    51. list[low] = list[i];
    52. list[i] = temp;
    53. quickSort(list, i + 1, high);
    54. quickSort(list, low, i - 1);
    55. }
    56. /** 使用Comparator接口的泛型快速排序 */
    57. public static void quickSort(E[] list, Comparatorsuper E> comparator) {
    58. quickSort(list, 0, list.length - 1, comparator);
    59. }
    60. /** 使用Comparator接口的泛型快速排序辅助方法 */
    61. private static void quickSort(E[] list, int low, int high, Comparatorsuper E> comparator) {
    62. if (low > high) return;
    63. E temp = list[low];
    64. int i = low;
    65. int j = high;
    66. while (i != j) {
    67. while (i < j && (comparator.compare(list[j], temp) > 0 || comparator.compare(list[j], temp) == 0)) j--;
    68. while (i < j && (comparator.compare(list[i], temp) < 0 || comparator.compare(list[i], temp) == 0)) i++;
    69. if (i < j) {
    70. E t = list[i];
    71. list[i] = list[j];
    72. list[j] = t;
    73. }
    74. }
    75. list[low] = list[i];
    76. list[i] = temp;
    77. quickSort(list, i + 1, high, comparator);
    78. quickSort(list, low, i - 1, comparator);
    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. }

     

  • 相关阅读:
    OCX 添加方法和事件 HTML调用ocx函数及回调 ocx又调用dll VS2017
    测试把minilua5.1源码文件转成图片
    零难度!台式电脑如何连接蓝牙耳机?简单几步完成
    java毕业设计开题报告基于SSM学生成绩管理系统
    arcgis pro模型构建器
    kotlin用ping命令判断网络是否是通的
    final关键字
    nodejs的安装和全局配置(超详细哦)
    Java 开发环境配置
    ABAP 选择屏幕多页签
  • 原文地址:https://blog.csdn.net/m0_62659797/article/details/126375224