(Latest Revision: 04/09/2003)
(04/09/2003 -- changed c_count++ to ++c_count to assure "true" is returned)
Comparing Sorts Program Gotchas
- You can take code like this:
if (theArray[currentIndex] > theArray[indexSoFar])
and turn it into code like this:
if (++c_count && (theArray[currentIndex] > theArray[indexSoFar]))
The expression "++c_count" takes care of incrementing the
counter and will not change the logic of the program. (Verify.)
Notice that order is important here. It does not work to put
"&& ++c_count" at the end of the original expression. (Why
not?)
You can use the same basic idea to take care of counts inside the
conditions of if-statements or counts inside loop-conditions.
- Use code like this:
cout << "mergesort starting ... " ;
cout.flush() ;
mergesort(A, 0, howMany-1, c_count, m_count) ;
cout << "done" << endl ;
To mark when the program starts a sort and finishes it. If you don't
use the cout.flush() command (on the suns) then the output
may be buffered and you won't see the "mergesort starting
..." part of the message until after the sort is
completed!
- It may be a good idea to make your counters unsigned long int. This
will give you a little extra margin in case you come close to
overflowing your counters. For example, you could declare your
counters this way:
unsigned long int c_count, m_count ;