| | |
Merge Sort
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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:
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?
C Syntax (Toggle Plain Text)
template <class T> void mergeSort(vector<T>& s) { mergeHelper(s.begin(), 0, s.size()); } template <class Itr> void mergeHelper(Itr start, unsigned int low, unsigned int high) { // If current element is the only element in the vector, return the element if (low + 1 == high) return; // Find the center of the current vector unsigned int center = ((high + low) / 2); // Sort lower half mergeHelper(start, low, center); // Sort upper half mergeHelper(start, center, high); // Combine consecutive sorted ranges [first, middle) and [middle, last) // into a single sorted range [first, last) inplace_merge(start + low, start + center, start + high); }
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.
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
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
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:
Thanks for the nudge in the right direction!
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:
C Syntax (Toggle Plain Text)
template <class T> void mergeSort(vector<T>& s) { vector<T>* temp = new vector<T>(s.size()); if(temp != NULL) { mergeHelper(s.begin(), 0, s.size(), temp->begin()); } delete temp; } template <class T> T max(int x, int y) { if(x > y) { return x; } else { return y; } } template <class Itr> void mergeHelper(Itr start, unsigned int low, unsigned int high, Itr temp) { if (low + 1 == high) return; else { unsigned int center = ((high + low) / 2); int l = low; int h = center; mergeHelper(start, low, center, temp); mergeHelper(start, center, high, temp); for(int i = 0; i < (high - low); i++) { if (l < center && (h == high || max(start[l], start[h]) == start[h])) { temp[i] = start[l]; l++; } else { temp[i] = start[h]; h++; } } for(i = low; i < high; i++) { start[i] = temp[i - low]; } } }
Thanks for the nudge in the right direction!
![]() |
Similar Threads
- Merge Sort Code Not Working Properly In Vc++.code Is Given (C++)
- hw assignment help on selection & merge sort (C)
- another merge sort question (C++)
- Merge sort (C++)
Other Threads in the C Forum
- Previous Thread: Problem with memory allocation =(
- Next Thread: Find complex no.
Views: 4313 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic executable fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation turboc unix user variable voidmain() wab win32 windows.h






