sorting dilemma

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: sorting dilemma

 
0
  #11
Sep 18th, 2008
Here's the spot I'm having trouble with inside the merge sort, I'm not sure what happens inside the statement now

  1. while((indexx < 50) && (indexy < 50) && (indexz < 50))
  2. {
  3. if (x[indexx] < y[indexy])
  4. {
  5. bigarray[indexbigarray] = x[indexx];
  6. indexx++; //increase the index
  7. }
  8. else
  9. {
  10. bigarray[indexbigarray] = y[indexy];
  11. indexy++; //increase the index
  12. }
  13. indexbigarray++; //move to the next position in the new array
  14. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: sorting dilemma

 
2
  #12
Sep 18th, 2008
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:
  1. void mergesort ( int a[], int b[], int dest[], int size_a, int size_b );
use the size parameters as limits on index_a and index_b in the loops.
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: sorting dilemma

 
0
  #13
Sep 18th, 2008
Okay, I got it, worked like a champ. Thanks for the advice, it worked fine.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC