Algorithm - Merge Sort(Java)

浅浅的花香味﹌ 2022-05-22 00:19 275阅读 0赞

分享一个大牛的人工智能教程。零基础!通俗易懂!风趣幽默!希望你也加入到人工智能的队伍中来!请点击http://www.captainbed.net

  1. package chimomo.learning.java.algorithm.sort;
  2. import java.util.Arrays;
  3. /**
  4. * @author Chimomo
  5. *
  6. * <p>
  7. * Introduction:
  8. *
  9. * Merge Sort is built on Merge operation,
  10. * it is a typical application of "Divide and Conquer" strategy.
  11. * The sorting speed of Merge Sort is second only to Quick Sort,
  12. * Generally applied on the sequence
  13. * that overall unordered but relatively ordered on its subsequences.
  14. * </p>
  15. *
  16. * <p>
  17. * Algorithm:
  18. *
  19. * The principle of Merge operation:
  20. * 1. Apply for memory to store the merged sequence,
  21. * its size equals to the sum of the two ordered sequences.
  22. * 2. Set two pointers,
  23. * each points to the start position of the ordered sequence separately.
  24. * 3. Compare the elements the two pointers pointed,
  25. * put the smaller one into merged sequence,
  26. * and move the pointer to the next position.
  27. * 4. Repeat step 3 until one pointer exceeds the rear of the sequence.
  28. * 5. Copy the remaining elements of the other sequence
  29. * to the rear of the merged sequence.
  30. *
  31. * The principle of Merge Sort (suppose sequence has n elements):
  32. * 1. Divide: divide n elements into 2 subsequences
  33. * and each subsequence contains ceil(n/2) elements at most.
  34. * 2. Conquer: merge sort each subsequence recursively.
  35. * 3. Merge: merge the two ordered sequences into one ordered sequence.
  36. * </p>
  37. *
  38. * <p>
  39. * Stability:
  40. *
  41. * Merge Sort is a stable sorting algorithm.
  42. * </p>
  43. */
  44. public class MergeSort {
  45. private MergeSort() {
  46. }
  47. /**
  48. * Merge Sort
  49. *
  50. * @param a The array to be sorted
  51. * @param low The low index of array a
  52. * @param high The high index of array a
  53. */
  54. private static void mergeSort(int[] a, int low, int high) {
  55. int mid = (low + high) / 2;
  56. if (low < high) {
  57. mergeSort(a, low, mid);
  58. mergeSort(a, mid + 1, high);
  59. merge(a, low, mid, high);
  60. System.out.println(Arrays.toString(a));
  61. }
  62. }
  63. /**
  64. * The 2-way merge operation: merge two ordered sequences to one ordered sequence
  65. *
  66. * @param a The array to be merged
  67. * @param low The start subscript of the first ordered sequence
  68. * @param mid The end subscript of the first ordered sequence
  69. * (so the start subscript of the second ordered sequence is "mid + 1")
  70. * @param high The end subscript of the second ordered sequence
  71. */
  72. private static void merge(int[] a, int low, int mid, int high) {
  73. // Apply for memory whose size is the sum of the two ordered sequences,
  74. // this memory stores the merged sequence.
  75. int[] tmp = new int[high - low + 1];
  76. // Set two pointers,
  77. // their initial values are the starting positions of the two ordered sequence.
  78. int i = low;
  79. int j = mid + 1;
  80. int k = 0;
  81. // Repeat the following operations until one pointer exceeds the rear of sequence:
  82. // Comparing the elements the two pointers pointed,
  83. // put the smaller one into the merged sequence,
  84. // and move the pointer to the next position.
  85. while (i <= mid && j <= high) {
  86. if (a[i] < a[j]) {
  87. tmp[k++] = a[i++];
  88. } else {
  89. tmp[k++] = a[j++];
  90. }
  91. }
  92. // Copy all the remaining elements of the other sequence to the rear of the merged sequence
  93. while (i <= mid) {
  94. tmp[k++] = a[i++];
  95. }
  96. while (j <= high) {
  97. tmp[k++] = a[j++];
  98. }
  99. // Copy the merged sequence back to a
  100. System.arraycopy(tmp, 0, a, low, tmp.length);
  101. }
  102. /**
  103. * Test program
  104. *
  105. * @param args The arguments
  106. */
  107. public static void main(String[] args) {
  108. int[] a = {11, 16, 22, -3, 99, 0, 16, 6, -1, 99};
  109. System.out.println("Before merge sort:");
  110. System.out.println(Arrays.toString(a));
  111. System.out.println("In merge sort:");
  112. MergeSort.mergeSort(a, 0, a.length - 1);
  113. System.out.println("After merge sort:");
  114. System.out.println(Arrays.toString(a));
  115. }
  116. }
  117. /* ------ Running Results ------
  118. Before merge sort:
  119. [11, 16, 22, -3, 99, 0, 16, 6, -1, 99]
  120. In merge sort:
  121. [11, 16, 22, -3, 99, 0, 16, 6, -1, 99]
  122. [11, 16, 22, -3, 99, 0, 16, 6, -1, 99]
  123. [11, 16, 22, -3, 99, 0, 16, 6, -1, 99]
  124. [-3, 11, 16, 22, 99, 0, 16, 6, -1, 99]
  125. [-3, 11, 16, 22, 99, 0, 16, 6, -1, 99]
  126. [-3, 11, 16, 22, 99, 0, 6, 16, -1, 99]
  127. [-3, 11, 16, 22, 99, 0, 6, 16, -1, 99]
  128. [-3, 11, 16, 22, 99, -1, 0, 6, 16, 99]
  129. [-3, -1, 0, 6, 11, 16, 16, 22, 99, 99]
  130. After merge sort:
  131. [-3, -1, 0, 6, 11, 16, 16, 22, 99, 99]
  132. */

发表评论

表情:
评论列表 (有 0 条评论,275人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Algorithm入门

    Algorithm入门 什么是Algorithm? Algorithm(算法)是计算机科学中的一个重要概念,用于解决问题和执行任务的一系列指令或步骤。它是计算机科学

    相关 merge

    \--更新备注 merge into cust\_account\_im t using ( select nvl(aa.contact,cc.full\_name