Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Confused about a bit of code.

Name: Anonymous 2010-03-23 16:12

http://pastebin.org/121306

Could a friendly soul explain to me why removing/changing the for-loop at line 68-69 changes the whole program?
It changes output from   1 2 3 4 5   to   3 2 1 5 4 
if the input is 5 4 3 2 1

Name: Anonymous 2010-03-23 16:32

what do you change it nto

Name: Anonymous 2010-03-23 16:32

IHBT

Name: Anonymous 2010-03-23 16:38

I'm not a friendly soul, but what the hell. Lines 61-62.

Name: Anonymous 2010-03-23 16:49

>>2
Well, removing it entirely changes the behaviour into what I described up top.
If I change it to for example only go to A.length - 2, then it prints out something like 22354

But I don't understand why it has any effect on the return output? :/

>>4
What about lines 61-62?

Name: Anonymous 2010-03-23 17:04

I don't actually know Java, but
>>5
You don't store a pointer to the sorted results, so when you merge L and R you are merging two unsorted lists.

for(int i = 0; i < q; i++)
    L[i] = A[i];

Doesn't Java have some sort of copy method for Arrays? Disgusting.

int output[] = new int[A.length];
output = merge(L, R, output);

Isn't this constructor call wasted?

Name: Anonymous 2010-03-23 17:56

As for the pointer. It doesn't seem to be needed. I tried that solution at first, but currently, it gives the correct result as it is.

As for the copy? I don't know. Not that into Java yet.

As for the constructor? I think you're right.


But I don't think that explains the anomaly I'm experiencing.

Name: Anonymous 2010-03-23 18:09

>>6-7
Java has System.arraycopy(src, src_start, dest, dest_start, length).  It's probably the best simple copy function you can implement in the language.

Name: Anonymous 2010-03-23 18:20

>>8
Oh? I would've thought their array copying methods were in the Collections library. I know it has some ways of converting them and whatnot.
Guess that's why I didn't find it.

Name: Anonymous 2010-03-23 18:46

>>8
System.arraycopy
lol idiotic hierarchy

Name: Anonymous 2010-03-24 10:58

This appears to be an almost direct translation of the Wikipedia article http://en.wikipedia.org/wiki/Merge_sort .
But it is horribly written. Functions that are always used in the form of a = f(a) can often be rewritten to be less ludicrous, especially merge(), as it returns the temp argument it is given (see also line 66).
Infactually, both functions return one of their arguments. This is probably why the for at 68-69 is even necessary.
Stupid Java DEVELOPERS

Name: Anonymous 2011-02-04 18:29

Don't change these.
Name: Email:
Entire Thread Thread List