
-----------------------------------
Zren
Fri May 08, 2009 3:08 pm

Merge Sort &gt; Arrays.copyOfRange
-----------------------------------
What seems to be wrong here. I'm getting an error for:
left = MergeSort( Arrays.copyOfRange( a, 0, middle-1 ) );

java.lang.IllegalArgumentException: 0 > -1
at java.util.Arrays.copyOfRange(Arrays.java:3136)

Is it my logic or is it just me using the method improperly. I think it's my logic but I'm not entirely sure.


    public static int

-----------------------------------
Vermette
Fri May 08, 2009 4:15 pm

Re: Merge Sort &gt; Arrays.copyOfRange
-----------------------------------
Trace your code with an array of length 2.

edit:  Also, line 3136?!??  Are you trying to build a [url=http://en.wikipedia.org/wiki/God_object]God Object?

-----------------------------------
Zren
Sat May 09, 2009 2:19 pm

Re: Merge Sort &gt; Arrays.copyOfRange
-----------------------------------
It's line 3163 in the java.util.Arrays class dude. Tell the people who design the Java API that.

-----------------------------------
DemonWasp
Sat May 09, 2009 4:39 pm

RE:Merge Sort &gt; Arrays.copyOfRange
-----------------------------------
Remember that array.length > index of last element in the array for arrays indexed from 0 (like Java). Your second copyOfRange is the one that's causing the issue, I think.

-----------------------------------
Vermette
Sat May 09, 2009 9:53 pm

Re: RE:Merge Sort &gt; Arrays.copyOfRange
-----------------------------------
Remember that array.length > index of last element in the array for arrays indexed from 0 (like Java). Your second copyOfRange is the one that's causing the issue, I think.

Nah it's definitely the first use.  Like I said, try an array of length 2.

edit: Zren, apologies; I read fast and assumed the line number was from your own code.

-----------------------------------
riveryu
Thu May 14, 2009 7:41 pm

Re: Merge Sort &gt; Arrays.copyOfRange
-----------------------------------
I think the problem may lie with your interpretation of merge sort. 

- When you used "Arrays.copyOfRange" and passing it recursively, it simply creates an entirely new array and does not relate back the original array to be sorted. It was a good try at simplifying your code but you cannot do that...

- Merge sort assumes 2 sublists are sorted, but they are not sorted all together even if the last elements follow each other. 
Thats why you have merge method to put them together and keep them sorted.

Consider:
left   1 2 3 4
right 1 2 3 5
According to your code, it will append right to left yielding 1 2 3 4 1 2 3 5, which is not what you want....

You should look at more stuff on the logic and idea behind merge sort and recursion. 
Hope this helps.

-----------------------------------
Vermette
Thu May 14, 2009 8:28 pm

Re: Merge Sort &gt; Arrays.copyOfRange
-----------------------------------
Ok I guess it has been long enough.  Here is what I am referring to:

if ( a.length 