Midterm Answers 1. (a) O(N^2). (b) Clearly computer B solves the 100,000 element problem in 1 sec. Since we have a quadratic algorithm, 4 times the input implies 4^2 the running time, so in 16 sec. computer B can solve a 400,000 element problem. (c) There are O(N) find and insert operations at O(log N) cost per operation, so the total is O(N log N). 2. template Comparable mostFrequent( const vector & v ) { map m; for( int i = 0; i < v.size( ); i++ ) m[ v[i] ]++; map::iterator itr, max; max = itr = m.begin( ); for( itr++; itr != m.end( ); itr++ ) if( (*itr).second > (*max).second ) max = itr; return (*max).first; } 3. template void SplitList( const list & theList, list & evens, list & odds ) { list::const_iterator itr; // Alias test if( &theList == &evens || &theList == &odds || &evens == &odds ) throw AliasException( ); // assume this class exists // Make outputs empty while( !evens.empty( ) ) evens.pop_front( ); while( !odds.empty( ) ) odds.pop_front( ); int position = 1; for( itr = theList.begin( ); itr != theList.end( ); itr++ ) if( position++ % 2 == 0 ) // even position evens.push_back( *itr ); else odds.push_front( *itr ); } 4. iv, ii, iv, i, iv