944,179 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 6253
  • C RSS
Feb 27th, 2007
0

Merge Sort

Expand Post »
I am trying to learn how to write a merge sort function, but I have only gotten so far with the book I am using now:

  1. template <class T>
  2. void mergeSort(vector<T>& s) { mergeHelper(s.begin(), 0, s.size()); }
  3.  
  4.  
  5. template <class Itr>
  6. void mergeHelper(Itr start, unsigned int low, unsigned int high) {
  7.  
  8. // If current element is the only element in the vector, return the element
  9. if (low + 1 == high) return;
  10.  
  11. // Find the center of the current vector
  12. unsigned int center = ((high + low) / 2);
  13.  
  14. // Sort lower half
  15. mergeHelper(start, low, center);
  16.  
  17. // Sort upper half
  18. mergeHelper(start, center, high);
  19.  
  20. // Combine consecutive sorted ranges [first, middle) and [middle, last)
  21. // into a single sorted range [first, last)
  22. inplace_merge(start + low, start + center, start + high);
  23. }

I understand "what" the inplace merge function is supposed to do...but I'll be damned if I can figure out how to write the code for it and I have been unsuccessful in my online searches.

How would I write the code for my own inplace_merge function?
Last edited by FC Jamison; Feb 27th, 2007 at 3:23 am.
Similar Threads
Team Colleague
Reputation Points: 92
Solved Threads: 21
Posting Pro in Training
FC Jamison is offline Offline
436 posts
since Jun 2004
Feb 27th, 2007
0

Re: Merge Sort

Something like this ?
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Feb 28th, 2007
0

Re: Merge Sort

Yeah...like that.

I actually saw that in my research, but didn't think I could convert it to what I needed.

Your suggestion prompted me to look at the code a little closer, and I came up with this solution:

  1. template <class T>
  2. void mergeSort(vector<T>& s) {
  3. vector<T>* temp = new vector<T>(s.size());
  4. if(temp != NULL) { mergeHelper(s.begin(), 0, s.size(), temp->begin()); }
  5. delete temp;
  6. }
  7.  
  8.  
  9. template <class T>
  10. T max(int x, int y) {
  11. if(x > y) { return x; }
  12. else { return y; }
  13. }
  14.  
  15.  
  16. template <class Itr>
  17. void mergeHelper(Itr start, unsigned int low, unsigned int high, Itr temp) {
  18. if (low + 1 == high) return;
  19. else {
  20. unsigned int center = ((high + low) / 2);
  21. int l = low;
  22. int h = center;
  23.  
  24. mergeHelper(start, low, center, temp);
  25. mergeHelper(start, center, high, temp);
  26.  
  27. for(int i = 0; i < (high - low); i++) {
  28. if (l < center && (h == high || max(start[l], start[h]) == start[h])) {
  29. temp[i] = start[l];
  30. l++;
  31. }
  32. else {
  33. temp[i] = start[h];
  34. h++;
  35. }
  36. }
  37.  
  38. for(i = low; i < high; i++) { start[i] = temp[i - low]; }
  39. }
  40. }

Thanks for the nudge in the right direction!
Team Colleague
Reputation Points: 92
Solved Threads: 21
Posting Pro in Training
FC Jamison is offline Offline
436 posts
since Jun 2004
Mar 1st, 2007
0

Re: Merge Sort

Click to Expand / Collapse  Quote originally posted by FC Jamison ...
Thanks for the nudge in the right direction!
You are welcome.....
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Problem with memory allocation =(
Next Thread in C Forum Timeline: Find complex no.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC