Computer Science Canada Help With MergeSort |
Author: | whoareyou [ Sun Apr 08, 2012 9:37 pm ] | ||
Post subject: | Help With MergeSort | ||
I have a somewhat working merge sort class (I fixed the stack over flow problem) that can perform merge sort (obviously). I think that after many trials, I've gotten it to work with 4 numbers (100% correct). However, when I try anything that is 5 and above (ie. 5 numbers in the array or more) it fails. Sometimes it works (I've been testing with 5 only) and sometimes it doesn't. For example, it sorts "0 2 3 4 0" right, but it doesn't sort "3 0 1 2 1" properly. It outputs "0 1 1 1 2". I think something is wrong with the indexes somewhere because in most cases, it is just one number that is being overwritten with another one in the array. I am, however, unable to diagnose the problem so I was hoping someone could tell me what is wrong in the code:
|
Author: | 2goto1 [ Mon Apr 09, 2012 1:19 pm ] | ||
Post subject: | Re: Help With MergeSort | ||
When you say that you're unable to try and diagnose the problem, what have you tried to diagnose the problem with? How did you conclude that you're unable to diagnose the problem? This line:
Looks like integer division. With integer division operations in Java, remainders are truncated. 3 / 2 = 1. Could that be an issue? |
Author: | whoareyou [ Mon Apr 09, 2012 5:29 pm ] |
Post subject: | RE:Help With MergeSort |
Ok so I fixed one part: there was something wrong in the merge method with the length of the temp array. I wrote a program to test the actual merging process and it seems to work. So now, its clearly something wrong in the Merge_Sort method. The integer division has already been taken care of. That's why there's the +1 when calling the merge_sort for the right portion of the split array. |