Computing the Max

Another simple calculation is that of finding the largest element in a list.

This is an obvious $\Theta(n)$ operation... in fact, the total number of comparisons is just n-1.

int max( vector<int> & data )
{
  int max = data[0];
  for( int i = 1; i < data.size(); i++ )
    if( data[i] > max )
      max = data[i];
  
  return max;
}


next up previous
Next: Computing Max and Min Up: EXTREMALS Previous: EXTREMALS