(Latest Revision: 02/28/2010)

Comparing Sorts Program Help and Hints

(This information is C++ oriented, but it may also be of some use to Java programmers.)
  1. 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.

  2. 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 something like the cout.flush() command (on most computers) then the output may be buffered - and you won't see the "mergesort starting ..." part of the message until after the sort is completed!

  3. 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 ;