调试:归并排序

huangapple go评论124阅读模式
英文:

Debugging : Mergesort

问题

  1. 尝试在Java中实现归并排序我在脑海中反复检查了我的代码觉得它应该是可以工作的但显然我做错了些什么以下是代码部分
  2. ```java
  3. public static void mergeSort(int[] input, int start, int end) {
  4. if (end - start < 2)
  5. return;
  6. int mid = (start + end) / 2;
  7. mergeSort(input, start, mid);
  8. mergeSort(input, mid + 1, end);
  9. merge(input, start, mid, end);
  10. }
  11. public static void merge(int[] input, int start, int mid, int end) {
  12. if (input[mid - 1] <= input[mid])
  13. return;
  14. int i = start;
  15. int j = mid;
  16. int tempIndex = 0;
  17. int[] temp = new int[end - start]; //合并两个数组的大小
  18. /* 如果 i >= mid,意味着数组的左部分已经排序完成,
  19. 反之如果 j >= end,那么右部分也是一样的。
  20. 一旦发生这种情况,我们可以将剩余的元素直接复制到临时数组中。
  21. */
  22. while (i < mid && j < end) {
  23. temp[tempIndex++] = (input[i] <= input[j]) ? input[i++] : input[j++];
  24. }
  25. // 复制左部分中剩余的元素
  26. while (i < mid)
  27. temp[tempIndex++] = input[i++];
  28. // 复制右部分中剩余的元素
  29. while (j < end)
  30. temp[tempIndex++] = input[j++];
  31. // 将排序后的临时数组复制回原数组
  32. // 这是我认为可能出错的地方
  33. for (int k = 0; k < temp.length; k++) {
  34. input[start + k] = temp[k];
  35. }
  36. }

我认为问题可能出在没有正确地将临时数组中的排序元素复制回原始数组,但我不太确定。我在代码中加了注释来解释我的逻辑。

  1. <details>
  2. <summary>英文:</summary>
  3. Trying to implement merge sort in Java. I&#39;ve gone over my code a bunch in my head and I feel like it should be working, but obviously I&#39;m doing something wrong. Heres the code
  1. public static void mergeSort(int[] input, int start, int end) {
  2. if (end - start &lt; 2)
  3. return;
  4. int mid = (start + end) / 2;
  5. mergeSort(input, start, mid);
  6. mergeSort(input, mid + 1, end);
  7. merge(input, start, mid, end);
  8. }
  9. public static void merge(int[] input, int start, int mid, int end) {
  10. if (input[mid - 1] &lt;= input[mid])
  11. return;
  12. int i = start;
  13. int j = mid;
  14. int tempIndex = 0;
  15. int[] temp = new int[end - start]; //combined size of both arrays being merged
  16. /*if i is &gt;= mid, then that means the left portion of the array is done being sorted
  17. and vice versa if j &gt;= end. Once this happens, we should be able to
  18. just copy the remaining elements into the temp array
  19. */
  20. while (i &lt; mid &amp;&amp; j &lt; end) {
  21. temp[tempIndex++] = (input[i] &lt;= input[j]) ? input[i++] : input[j++];
  22. }
  23. //Copying left over elements in left portion
  24. while (i &lt; mid)
  25. temp[tempIndex++] = input[i++];
  26. //Copying left over elements in right portion
  27. while (j &lt; end)
  28. temp[tempIndex++] = input[j++];
  29. //Copy the sorted temp array into the original array
  30. //This is where I think I must be messing up
  31. for (int k = 0; k &lt; temp.length; k++) {
  32. input[start + k] = temp[k];
  33. }
  34. }
  1. I think it must be that im not copying correctly the temp array with the sorted elements back into the original array, but I&#39;m not sure. I wrote comments on my code explaining my logic.
  2. </details>
  3. # 答案1
  4. **得分**: 2
  5. 看一下以下的变更:
  6. 1. 计算中间值
  7. &gt; int mid = start + (end - start) / 2;
  8. 2. 正确地分配指针 i j
  9. &gt; int i = start;
  10. &gt; int j = mid+1;
  11. 3. 正确的临时数组大小。
  12. &gt; int [] temp = new int[end-start+1];
  13. 4. 修正了代码中的 while 循环条件。
  14. class Solution{
  15. public static void mergeSort(int[] input, int start, int end)
  16. {
  17. if (end == start ) return;
  18. int mid = start + (end - start) / 2;
  19. mergeSort(input, start, mid);
  20. mergeSort(input, mid+1, end);
  21. merge(input, start, mid, end);
  22. }
  23. public static void merge(int[] input, int start, int mid, int end) {
  24. // 下面这个指令是不必要的
  25. // if(input[mid-1] &lt;= input[mid]) return;
  26. int i = start;
  27. int j = mid+1;
  28. int tempIndex = 0;
  29. int [] temp = new int[end-start+1]; // 被合并的两个数组的总大小
  30. /* 如果 i &gt;= mid,则意味着数组的左部分已经排序完成,反之亦然,如果 j &gt;= end,则意味着数组的右部分已经排序完成。一旦发生这种情况,我们应该能够将剩余的元素直接复制到临时数组中 */
  31. while(i &lt;= mid &amp;&amp; j &lt;= end){
  32. temp[tempIndex++] = (input[i] &lt;= input[j]) ? input[i++] : input[j++];
  33. }
  34. // 复制左部分中剩余的元素
  35. while(i &lt;= mid)
  36. temp[tempIndex++] = input[i++];
  37. // 复制右部分中剩余的元素
  38. while(j &lt;= end)
  39. temp[tempIndex++] = input[j++];
  40. // 将排序好的临时数组复制到原始数组中
  41. // 这是我认为我可能搞错的地方
  42. for(int k = 0; k &lt; temp.length; k++){
  43. input[start+k] = temp[k];
  44. }
  45. }
  46. public static void main(String[] args){
  47. int[] input = {9,4,6,8,5,7,0,2};
  48. mergeSort(input,0,7);
  49. for(int i : input)
  50. System.out.println(i);
  51. }
  52. }
  53. <details>
  54. <summary>英文:</summary>
  55. Take a look at the following changes:
  56. 1. Calculating mid
  57. &gt; int mid = start + (end - start) / 2;
  58. 2. Assigning pointers i,j correctly.
  59. &gt; int i = start;
  60. &gt; int j = mid+1;
  61. 3. Correct size of temp array.
  62. &gt; int [] temp = new int[end-start+1];
  63. 4. Corrected while loops condition in the code.
  64. class Solution{
  65. public static void mergeSort(int[] input, int start, int end)
  66. {
  67. if (end == start ) return;
  68. int mid = start + (end - start) / 2;
  69. mergeSort(input, start, mid);
  70. mergeSort(input, mid+1, end);
  71. merge(input, start, mid, end);
  72. }
  73. public static void merge(int[] input, int start, int mid, int end) {
  74. // No Need of the under mentioned instruction
  75. // if(input[mid-1] &lt;= input[mid]) return;
  76. int i = start;
  77. int j = mid+1;
  78. int tempIndex = 0;
  79. int [] temp = new int[end-start+1]; //combined size of both arrays being merged
  80. /*if i is &gt;= mid, then that means the left portion of the array is done being sorted and vice versa if j &gt;= end. Once this happens, we should be able to just copy the remaining elements into the temp array */
  81. while(i &lt;= mid &amp;&amp; j &lt;= end){
  82. temp[tempIndex++] = (input[i] &lt;= input[j]) ? input[i++] : input[j++];
  83. }
  84. //Copying left over elements in left portion
  85. while(i &lt;= mid)
  86. temp[tempIndex++] = input[i++];
  87. //Copying left over elements in right portion
  88. while(j &lt;= end)
  89. temp[tempIndex++] = input[j++];
  90. //Copy the sorted temp array into the original array
  91. //This is where I think I must be messing up
  92. for(int k = 0; k &lt; temp.length; k++){
  93. input[start+k] = temp[k];
  94. }
  95. }
  96. public static void main(String[] args){
  97. int[] input = {9,4,6,8,5,7,0,2};
  98. mergeSort(input,0,7);
  99. for(int i : input)
  100. System.out.println(i);
  101. }
  102. }
  103. </details>

huangapple
  • 本文由 发表于 2020年9月15日 12:35:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63895157.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定