Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Merge Sort - 'almost' working

Hello everyone!


I have been learning how to program for a while and I thought it came time for algorithms. I bought Cormen's book and everything was going smoothly until it came to Merge Sort. I understand the theory but I am having trouble putting it into practise. I found online a few (working) examples (like this one: http://goo.gl/75R0z) and I tried to look up to them and write my own code (with slight changes) and so far so unsuccessful. The code compiles but it doesn't do it's thing. Can you give me a hand?


main function:
http://pastebin.com/y0HZ7Dkj


Here's the algorithm:
http://pastebin.com/U8XAKsiv

Posted on Apr 1, 2013 11:33 AM

Reply
22 replies

Apr 2, 2013 5:33 AM in response to Keith Barkley

sP = startingPoint
eP = endingPoint
mP = middlePoint


May not be correct in terms of English, but seems to talk to my programming imagination just fine.


I hope it is clearer now.


The comparison takes place in the first while-loop. To be exact it's the if-statement within the loop. Elements are compared and copied to the temporary array. When done sorting (at least theoretically), everything from the tempArr is copied back to the original array (see: the for-loop at the end).

Apr 2, 2013 8:12 AM in response to Keith Barkley

Isn't it a pointer to an array?


I read it like this:


short* array = new short[...]


"array" is a pointer, which can point at variables of short type (just like a regular pointer eg. short* ptr), which points at (operator new returns an address) an array of shorts (hence short[...]).


I haven't tried, but an array of pointers wouldn't look like this


short** array = new (short*)[...]


that would be:
"array" is a pointer, which can point at elements of type (short*) (therefore it can point at pointers), which points at an array of pointers


Correct me if I'm wrong.

Apr 2, 2013 8:34 AM in response to Keith Barkley

This is not objective C. This is C++
"new", cutting long story short, does in C++ what malloc does in C


type name[no_of_el] - yes, this is an arrray, but there's a problem. Try this:


short someVariable = 10;
short someArray[someVariable];


Using above method, the length of the array has to be known at compilation time, whereas pointers can be allocated and deallocated (new/delete and new[]/delete[]) dynamically. In this case you need to go:


short* pointer2array = new short[someVariable];

Apr 2, 2013 9:59 AM in response to BR41N-FCK

If it is C++ then why don't you just use the "sort" algorithm?


I used to be a big fan of C++ until it finally dawned on me I was just wasting my time with a truly awful language.


The first thing to do in a situation like this is print out your array before and after. That will give you a much better idea of what is going on. You can still using printf() in C++. There is no reason to torture yourself with iostream. Once you do that, you will see that you are actually overflowing your data. The problem line is this one:


for (short var = sP; var <= eP; ++var) array[var] = tempArr[var];


In this function sP can start at any location in the original array but tempArr always starts at 0. Re-use that temp_iter for tempArr and start it at 0. That will take care of it and transform this:


/tmp $ ./a.out

-74 -49 -68 -86 -28 91 25 -62 -96 -71 -29 26 28 -78 23 -30 -21 -33 -74 39 41

-6868 -30677 -74 -74 -74 -74 0 0 0 0 0 0 2 0 0 0 0 0 91 -6868 256


to this:


/tmp $ ./a.out

6 54 -55 64 -44 80 68 -3 -37 -39 47 -49 -92 -21 92 -79 92 33 2 -33 61

-92 -79 -55 -49 -44 -39 -37 -33 -21 -3 2 6 33 47 54 61 64 68 80 92 92

Apr 2, 2013 10:11 AM in response to etresoft

If I had a project with a short deadline - sure, I'd use a pre-made (what else can we call it?) solution. But right now I am learning algorithms, so there's no point using ready ones.


Thank you for solving my problem. It was so simple... and maybe that's why I have overlooked it :-)


Speaking of C++. Why would you call it awful? I mean it isn't the best for developing windowed apps (see: objective-c and c# on windows), but it is great for console ones and also it is good for education purposes thanks to its plain syntax (compared to c# and not to even mention the crazy syntax of objective-c).

Apr 2, 2013 10:31 AM in response to BR41N-FCK

Sorting algorithms are *extremely* fine tuned. By changing any thing substantive (i.e., putting in something other than a print statement) you are probably changing it into some *other* sorting algorithm, or simply breaking it.


One reason I like Sedgewick is that he has really good summaries of the algorithms and good illustrations of the metrics.


Also, random data is not necessarily the best for sorting. Sedgwick always starts by sorting "ASORTINGEXAMPLE" so you can tell at a glance whether it is working.

Apr 2, 2013 10:43 AM in response to BR41N-FCK

BR41N-FCK wrote:


If I had a project with a short deadline - sure, I'd use a pre-made (what else can we call it?) solution. But right now I am learning algorithms, so there's no point using ready ones.


The only reason for the sorting and linked-list exercises is to give students some experience with challenging, but solvable problems in programming. Don't get me wrong, there is great value in that. However, it would be better done in C or even Pascal. Computer Science teaching kind of went off the deep end in 1990 or and has only gotten worse.


Thank you for solving my problem. It was so simple... and maybe that's why I have overlooked it :-)


One of the skills a programmer must have is how to debug such problems. That doesn't always mean running in a debugger. I very rarely use a debugger. Most modern, UI or server work is far too complex for such things. Simple "printf" statements are usually all that is required.


Speaking of C++. Why would you call it awful? I mean it isn't the best for developing windowed apps (see: objective-c and c# on windows), but it is great for console ones and also it is good for education purposes thanks to its plain syntax (compared to c# and not to even mention the crazy syntax of objective-c).


Because I wasted so much of my programming career on it. I suppose it was a valuable lesson. I suspose if someone had told me it was junk in 1995 I probably would have ignored them then. Now I have a decent store of tacit wisdom that is hard to explain. I just "know" when someone does or doesn't know what they are talking about. Just because "everybody is doing it" doesn't mean everybody isn't wrong.


Objective-C syntax is actually quite simple. It is just the message passing through brackets. That is insignificant compared to cryptic C++11. You aren't even using C++ in this program. With the expection of iostream, you are only using C. I don't see any templates, no references, no const-correctness, no virtual functions, no inline code, no iterator traits, no lambdas, no locales, no iomanips. It's all junk that only makes life harder and life is short enough as it is.

Apr 2, 2013 11:01 AM in response to etresoft

Objective-C syntax is actually quite simple. It is just the message passing through brackets. That is insignificant compared to cryptic C++11.

When it comes to simplicity of syntax I think it is a matter of taste. Look at implementation of classes in those languages. As for me, C++ syntax wins.



You aren't even using C++ in this program. With the expection of iostream, you are only using C. I don't see any templates, no references, no const-correctness, no virtual functions, no inline code, no iterator traits, no lambdas, no locales, no iomanips. It's all junk that only makes life harder and life is short enough as it is.


My experience with programming isn't great (bare in mind I am not a professional programmer yet) but I wouldn't say the mechanisms you have mentioned make anything complicated unless you force yourself to use those in your code (which would be pointless). All elements of the language have their field of application, don't you think? (except for enum type and virtual functions, but that's just me)

Merge Sort - 'almost' working

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.