| | |
sorting dilemma
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Dec 2007
Posts: 226
Reputation:
Solved Threads: 1
Here's the spot I'm having trouble with inside the merge sort, I'm not sure what happens inside the statement now
C++ Syntax (Toggle Plain Text)
while((indexx < 50) && (indexy < 50) && (indexz < 50)) { if (x[indexx] < y[indexy]) { bigarray[indexbigarray] = x[indexx]; indexx++; //increase the index } else { bigarray[indexbigarray] = y[indexy]; indexy++; //increase the index } indexbigarray++; //move to the next position in the new array }
You could try writing the mergesort() to handle three input arrays, storing to a fourth bigarray, but that's an awkward way. We generally want to solve problems in terms of things that are already solved.
You know how to merge two arrays into one. There is no requirement that the two source arrays be of the same size, only that the destination be at least big enough to hold the total number of elements.
What if you have, in main, another array of at least size 100, which will be the temporary holding place for the result of merging x and y. Then, call mergesort( ) again, passing z and the temp array, with big array as the destination. This will require that mergesort() also be told the size of the source arrays. Side benefit is that now your mergesort will handle any size problem, not the just specific case of inputs of size 50. This more general approach means never having to revise the function when the problem scale changes. So, your prototype for mergesort ()might look like:
use the size parameters as limits on index_a and index_b in the loops.
You know how to merge two arrays into one. There is no requirement that the two source arrays be of the same size, only that the destination be at least big enough to hold the total number of elements.
What if you have, in main, another array of at least size 100, which will be the temporary holding place for the result of merging x and y. Then, call mergesort( ) again, passing z and the temp array, with big array as the destination. This will require that mergesort() also be told the size of the source arrays. Side benefit is that now your mergesort will handle any size problem, not the just specific case of inputs of size 50. This more general approach means never having to revise the function when the problem scale changes. So, your prototype for mergesort ()might look like:
C++ Syntax (Toggle Plain Text)
void mergesort ( int a[], int b[], int dest[], int size_a, int size_b );
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: please solve my problem
- Next Thread: Vector & reference Questions
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator givemetehcodez graph homeworkhelp iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multiple newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting spoonfeeding string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






