实现归并排序算法的具体问题

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

Specific problem with Implementation of a Merge Sort Algorithm

问题

  1. /**
  2. * 归并排序算法
  3. * @param array 要排序的数组
  4. * @param i - 开始排序的位置
  5. * @param j - 结束排序的位置
  6. */
  7. public void MergeSort(Movie[] array, int i, int j) {
  8. if (i < j) {
  9. int m = (i + j) / 2;
  10. MergeSort(array, i, m);
  11. MergeSort(array, m + 1, j);
  12. merge(array, m);
  13. }
  14. }
  15. void merge(Movie[] array, int m) {
  16. int p = 0;
  17. int q = m + 1;
  18. int r = 0;
  19. int j = array.length - 1;
  20. Movie[] temp = new Movie[array.length];
  21. while (p <= m && q <= j) {
  22. if (array[p].compareTo(array[q]) == 1) {
  23. temp[r++] = array[p++];
  24. } else {
  25. temp[r++] = array[q++];
  26. }
  27. }
  28. while (p <= m) {
  29. temp[r++] = array[p++];
  30. }
  31. while (q <= j) {
  32. temp[r++] = array[q++];
  33. }
  34. System.arraycopy(temp, 0, array, 0, temp.length);
  35. }

在测试 "a,b,c,d,e,h,y,z" 等情况时,输出结果如下...

  1. a |
  2. b |
  3. d |
  4. e |
  5. h |
  6. c |
  7. y |
  8. z |

显然,这并不是按字母顺序排列的,只是不能弄清楚为什么。

英文:

Java

I am attempting to write a merge sort algorithm in order to sort a list of custom objects into alphabetical order, I have now been looking at my code and the pseudocode for a couple hours and have yet to figure out why it isn't working,

Perhaps some fresh eyes could help,

My code is as follows...

  1. /**
  2. * Merge Sort Algorithm
  3. * @param array array to sort
  4. * @param i - point to start sorting at
  5. * @param j - point to end sorting at
  6. */
  7. public void MergeSort(Movie[] array, int i, int j) {
  8. if(i &lt; j){
  9. int m = (i+j)/2;
  10. MergeSort(array, i, m);
  11. MergeSort(array, m+1, j);
  12. merge(array,m);
  13. }
  14. }
  15. void merge(Movie[] array, int m){
  16. int p = 0;
  17. int q = m+1;
  18. int r = 0;
  19. int j = array.length-1;
  20. Movie[] temp = new Movie[array.length];
  21. while(p &lt;= m &amp;&amp; q &lt;= j){
  22. if(array

    .compareTo(array[q]) == 1){

  23. temp[r++] = array

    ;

  24. }else{

  25. temp[r++] = array[q++];

  26. }

  27. }

  28. while (p &lt;= m){

  29. temp[r++] = array

    ;

  30. }

  31. while (q &lt;= j){

  32. temp[r++] = array[q++];

  33. }

  34. System.arraycopy(temp,0,array,0,temp.length);

  35. }

When testing on cases of "a,b,c,d,e,h,y,z", the output is as follows...

  1. a |
  2. b |
  3. d |
  4. e |
  5. h |
  6. c |
  7. y |
  8. z |

Evidently this is not in alphabetical order, just can't figure out why

答案1

得分: 0

从您在评论中发布的伪代码中,我可以告诉您,您没有实现给定的伪代码。

Merge(A[i...j], m) 函数具有参数 m,即中间值,数组 A 以及 参数 ij,用于定义在该数组中进行合并的范围。在我看来,伪代码并不准确,质量不佳。

参数 ij 被用于初始化 pr。在您当前的实现中,您将两者都初始化为 0,这与伪代码不符。

您的 merge 方法需要这两个参数 ij 来定义合并的范围。

英文:

Looking at the pseudocode you've posted in a comment I can tell you that you haven't implemented that given pseudocode.

The Merge(A[i...j], m) function has the parameters m, which is the mid, the array A and the parameters i and j to define the bounds for the merging in that array. In my opinion the pseudocode is just not precise and bad.

The parameters i and j are used to initialize p and r. In your current implementation you are initializing both with 0, which is not what the pseudocode does.

Your method merge needs those two parameters i and j to define the bounds for the merging.

huangapple
  • 本文由 发表于 2020年5月5日 08:53:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/61604036.html
匿名

发表评论

匿名网友

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

确定