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

I don't get Mergesort

Name: Anonymous 2011-01-16 22:13

Seriously.

I can understand the general principle, i.e. Split up list into smaller bits (until they're only one element long) then merge back in correct order, but how is the merging performed? It looks, from the code examples that I'm looking at, that the merging should be in-efficient as all fuck. Can somebody tell me why it isn't?

(Any explanation you'd care to offer can assume array or linked list, either works for me)

Name: Anonymous 2011-01-16 22:38

say you are looking at two lists.  point to the first element of both lists.  start going down one of the lists comparing each element to the wherever the pointer in the second list is.  lets say we're doing ascending order.  if the element in the first list is less than the second, put it in a new list.  move the first pointer to the next element in the first list.  repeat until the element in the second list is greater than the first.

 when that happens put that element in the new list and move that pointer up to the next element in the second list.  repeat until you get to the end of one of the lists.  when that happens you can just empty the remaining elements into the new list because everything else had to have been less than those.

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