Ask your own question, for FREE!
Mathematics 12 Online
OpenStudy (anonymous):

So I've managed to understand all the basic \( O(n^2) \) (except shell sort, but that's a little different) basic comparison sort algorithms. However, although I understand the whole idea of mergesort, I still couldn't figure out how to code it in C. Can anyone help?

OpenStudy (anonymous):

Yes I could have, but you stole my mod power, so I won't :P

OpenStudy (anonymous):

?

OpenStudy (anonymous):

Merge sort isn't that hard to code.

OpenStudy (anonymous):

please.... you're like the guru of algorithms here.

OpenStudy (anonymous):

it shouldn't be (it's the easiest of the O(nlgn) sorting algorithms) but I'm still having trouble :(

OpenStudy (anonymous):

My personal preference is always quick sort.

OpenStudy (anonymous):

but I'm only at the merge sort part of the book (quicksort is much later in the book)

OpenStudy (anonymous):

http://www.sorting-algorithms.com/merge-sort

OpenStudy (across):

Do you like recursion?

OpenStudy (anonymous):

the algorithm on that page seems straightforward to translate over to C

OpenStudy (anonymous):

http://www.google.com/search?q=recursion

OpenStudy (anonymous):

I love recursion, but there is always a chance of stack overflow in recursion.

OpenStudy (anonymous):

I love recursive algorithms (too bad they don't perform as well as the iterative/functional version and also chance of exceeding recursion depth and overflowing the stack)

OpenStudy (anonymous):

For example if you are looking to implement O(ln) pow(a,b,c) the recursive approach often invoke stack overflow.

OpenStudy (anonymous):

pow(a,b,c) is finding \( a^b \% c \)

OpenStudy (anonymous):

this is what I've done in C: http://ideone.com/nZRKA it ran alright

OpenStudy (anonymous):

but it took me hours to stop all the segfaults

OpenStudy (anonymous):

I had most trouble with the merge step

OpenStudy (asnaseer):

you may be interested at looking at this: http://www.cprogramming.com/tutorial/computersciencetheory/mergesort.html

OpenStudy (asnaseer):

if you scroll down on that page and look for the "Implementation" section - they provide a simple one for you

OpenStudy (anonymous):

originally I had the merge step allocate two buffers for the left and right subarrays.

OpenStudy (anonymous):

I think I have something in my archive that I have implemented a year back.

OpenStudy (anonymous):

http://www.youtube.com/watch?v=t8g-iYGHpEA

OpenStudy (anonymous):

I can give you if you want ?

OpenStudy (anonymous):

sure

OpenStudy (anonymous):

I wanna go to bed tonight, knowing how to mergesort

OpenStudy (anonymous):

But it is the solution to this problem: www.spoj.pl/problems/MERGSORT/

OpenStudy (asnaseer):

want to mergesort in your sleep? :D

OpenStudy (anonymous):

haha :D

OpenStudy (anonymous):

I see algorithm pseudocode in my dreams

OpenStudy (asnaseer):

you need to see a doctor ;-)

OpenStudy (anonymous):

So this is how the merge step of mergesort works: 1. compare the elements at the beginning of each subarray, and place the smallest one in the output buffer. do this until one of the subarrays are empty 2. simply place all the elements in the non-empty subarray into the output buffer.

OpenStudy (anonymous):

that's it, right?

OpenStudy (anonymous):

then the sorting part is 1. divide the buffer in half 2. merge sort the left half, 3. merge sort the right half 4. merge them!

OpenStudy (anonymous):

oh and before all those: check to see if the array only has one element, and return if true (already sorted)

OpenStudy (anonymous):

This is the main part of my solution: http://ideone.com/fiY78 I have skimmed the fast i/o routine and the main(); I Hope you could make it work from this :)

OpenStudy (anonymous):

ooh it's even commented!

OpenStudy (anonymous):

Now logout and let me enjoy being mod :P

OpenStudy (anonymous):

Oh yes, I am a fool so I keep everything commented in my archive ;)

OpenStudy (anonymous):

I like the idea of mergesort: how it boldly calls itself "mergesort (left, mid); mergesort(mid+1, right);

OpenStudy (anonymous):

it seems to be capable of sorting a million 32 bit integers, unlike the isort I came up with :-P \[# void isort(void* base, size_t num, size_t size, int (*comparator) (const void*, const void*)) { void *left, *right, *key; if ((key = malloc(size))) { for (right = (base + size); (size_t) (right - base)/size < num; right += size) { memcpy(key, right, size); for (left = right; left > base && comparator((left - size), key) > 0; left -= size) { memcpy(left, left - size, size); } memcpy(left, key, size); } } free(key); }\]

OpenStudy (anonymous):

Are you going to logout now ? !!!

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
HeyItsAlicia: Why was questioncove not working??
2 hours ago 2 Replies 1 Medal
Countless7Echos: Ah trying out the whole T.V girl drawing :p (I love drawing eyes)
12 hours ago 14 Replies 6 Medals
kaelynw: starting to draw a hand
2 days ago 17 Replies 2 Medals
Twaylor: Rate it :D (Took 2 days)
6 days ago 7 Replies 0 Medals
XShawtyX: Art, Short Writing Assignment: Imagining Landscapes
2 days ago 9 Replies 1 Medal
XShawtyX: Chemistry, Help ud83dude4fud83cudffe
1 week ago 13 Replies 1 Medal
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!