Merge Sort

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2004
Posts: 434
Reputation: FC Jamison is on a distinguished road 
Solved Threads: 20
Team Colleague
FC Jamison's Avatar
FC Jamison FC Jamison is offline Offline
Posting Pro in Training

Merge Sort

 
0
  #1
Feb 27th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,652
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Merge Sort

 
0
  #2
Feb 27th, 2007
Something like this ?
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 434
Reputation: FC Jamison is on a distinguished road 
Solved Threads: 20
Team Colleague
FC Jamison's Avatar
FC Jamison FC Jamison is offline Offline
Posting Pro in Training

Re: Merge Sort

 
0
  #3
Feb 28th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,652
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Merge Sort

 
0
  #4
Mar 1st, 2007
Originally Posted by FC Jamison View Post
Thanks for the nudge in the right direction!
You are welcome.....
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 4313 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC