943,912 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 992
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Sep 18th, 2008
0

Re: sorting dilemma

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)
  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. }
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Sep 18th, 2008
2

Re: sorting dilemma

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:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Sep 18th, 2008
0

Re: sorting dilemma

Okay, I got it, worked like a champ. Thanks for the advice, it worked fine.
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: please solve my problem
Next Thread in C++ Forum Timeline: Vector & reference Questions





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


Follow us on Twitter


© 2011 DaniWeb® LLC