数组中排序

  1. public static void main(String[] args) {
  2. int[] nums = {1,3,5,2,7,6,9};
  3. Arrays.sort(nums);
  4. System.out.println("增序排列");
  5. for (int i = 0; i < nums.length; i++) {
  6. System.out.println("nums:" + i + " :" + nums[i]);
  7. }
  8. //要实现减序排序,得通过包装类型数组,基本类型数组是不行的
  9. Integer[] integers = new Integer[]{2,555,3,789,4,1,543};
  10. System.out.println("减序排列");
  11. //通过一个比较器,比较函数可以通过比较进而
  12. Arrays.sort(integers, new Comparator<Integer>() {
  13. @Override
  14. public int compare(Integer o1, Integer o2) {
  15. return o2 - o1;
  16. }
  17. });
  18. System.out.println("部分增序排列");
  19. for (int i = 0; i < nums.length; i++) {
  20. System.out.println("integers:" + i + " :" + integers[i]);
  21. }
  22. int[] ints = {1222,77,5,3,444,76,7};
  23. // 对数组的[2,5)位进行排序
  24. Arrays.sort(ints,2,5);
  25. for (int i = 0; i < ints.length; i++) {
  26. System.out.println("ints:" + i + " :" + ints[i]);
  27. }
  28. }

比较函数

一个比较函数,它对某些对象集合进行总排序。比较器可以传递给排序方法(例如Collections.sort或Arrays.sort )以允许精确控制排序顺序。

  1. package java.util;
  2. public class Arrays {
  3. // 排序
  4. public static void sort(int[] a) {
  5. DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
  6. }
  7. // 部分排序
  8. public static void sort(int[] a, int fromIndex, int toIndex) {
  9. rangeCheck(a.length, fromIndex, toIndex);
  10. DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
  11. }
  12. }

DualPivotQuicksort 双轴快速排序

  1. /**
  2. * Sorts the specified range of the array using the given
  3. * workspace array slice if possible for merging
  4. *
  5. * @param a the array to be sorted
  6. * @param left the index of the first element, inclusive, to be sorted
  7. * @param right the index of the last element, inclusive, to be sorted
  8. * @param work a workspace array (slice)
  9. * @param workBase origin of usable space in work array
  10. * @param workLen usable size of work array
  11. */
  12. /**
  13. *使用给定的
  14. *工作区数组切片(如果可能)用于合并
  15. *@param a要排序的数组
  16. *@param将第一个元素的索引(包括)留作排序
  17. *@param right要排序的最后一个元素(包括)的索引
  18. *@param工作空间数组(切片)
  19. *@param workBase工作阵列中可用空间的来源
  20. *@param worken工作数组的可用大小
  21. */
  22. static void sort(int[] a, int left, int right,
  23. int[] work, int workBase, int workLen) {
  24. // Use Quicksort on small arrays
  25. if (right - left < QUICKSORT_THRESHOLD) {
  26. sort(a, left, right, true);
  27. return;
  28. }
  29. /*
  30. * Index run[i] is the start of i-th run
  31. * (ascending or descending sequence).
  32. */
  33. int[] run = new int[MAX_RUN_COUNT + 1];
  34. int count = 0; run[0] = left;
  35. // Check if the array is nearly sorted
  36. for (int k = left; k < right; run[count] = k) {
  37. if (a[k] < a[k + 1]) { // ascending
  38. while (++k <= right && a[k - 1] <= a[k]);
  39. } else if (a[k] > a[k + 1]) { // descending
  40. while (++k <= right && a[k - 1] >= a[k]);
  41. for (int lo = run[count] - 1, hi = k; ++lo < --hi; ) {
  42. int t = a[lo]; a[lo] = a[hi]; a[hi] = t;
  43. }
  44. } else { // equal
  45. for (int m = MAX_RUN_LENGTH; ++k <= right && a[k - 1] == a[k]; ) {
  46. if (--m == 0) {
  47. sort(a, left, right, true);
  48. return;
  49. }
  50. }
  51. }
  52. /*
  53. * The array is not highly structured,
  54. * use Quicksort instead of merge sort.
  55. */
  56. if (++count == MAX_RUN_COUNT) {
  57. sort(a, left, right, true);
  58. return;
  59. }
  60. }
  61. // Check special cases
  62. // Implementation note: variable "right" is increased by 1.
  63. if (run[count] == right++) { // The last run contains one element
  64. run[++count] = right;
  65. } else if (count == 1) { // The array is already sorted
  66. return;
  67. }
  68. // Determine alternation base for merge
  69. byte odd = 0;
  70. for (int n = 1; (n <<= 1) < count; odd ^= 1);
  71. // Use or create temporary array b for merging
  72. int[] b; // temp array; alternates with a
  73. int ao, bo; // array offsets from 'left'
  74. int blen = right - left; // space needed for b
  75. if (work == null || workLen < blen || workBase + blen > work.length) {
  76. work = new int[blen];
  77. workBase = 0;
  78. }
  79. if (odd == 0) {
  80. System.arraycopy(a, left, work, workBase, blen);
  81. b = a;
  82. bo = 0;
  83. a = work;
  84. ao = workBase - left;
  85. } else {
  86. b = work;
  87. ao = 0;
  88. bo = workBase - left;
  89. }
  90. // Merging
  91. for (int last; count > 1; count = last) {
  92. for (int k = (last = 0) + 2; k <= count; k += 2) {
  93. int hi = run[k], mi = run[k - 1];
  94. for (int i = run[k - 2], p = i, q = mi; i < hi; ++i) {
  95. if (q >= hi || p < mi && a[p + ao] <= a[q + ao]) {
  96. b[i + bo] = a[p++ + ao];
  97. } else {
  98. b[i + bo] = a[q++ + ao];
  99. }
  100. }
  101. run[++last] = hi;
  102. }
  103. if ((count & 1) != 0) {
  104. for (int i = right, lo = run[count - 1]; --i >= lo;
  105. b[i + bo] = a[i + ao]
  106. );
  107. run[++last] = right;
  108. }
  109. int[] t = a; a = b; b = t;
  110. int o = ao; ao = bo; bo = o;
  111. }
  112. }

https://blog.csdn.net/github_38838414/article/details/80642329

注意:博客中的非27,根据源码为47