little help

need little help. I just want to count the number of comparisons used when called with a vector of size n.
vector<int> minmax( vector<int> V, int l, int u )
{
int m;
vector<int> M( 2 ), M1( 2 ), M2( 2 );

if( TRACING )
{
cout << "( " << l <<"," << u << " )" << endl;
}

if( u == l ) // one element - no comparisons needed
{
M[0] = V[l]; M[1] = V[u];
return M;
}

if( u-l == 1 ) // two elements - only one comparison
{
if( V[l] <= V[u] )
{
M[0] = V[l]; M[1] = V[u];
}
else
{
M[0] = V[u]; M[1] = V[l];
}
return M;
}


m =( u+l )/2;


M1 = minmax( V,l,m );
M2 = minmax( V,m+1,u );

if( TRACING )
{
cout << "M1 = " << M1[0] << "," << M1[1] << endl;
cout << "M2 = " << M2[0] << "," << M2[1] << endl;
}

M[0] = min( M1[0],M2[0] );
M[1] = max( M1[1],M2[1] );
return M;
}

int main()
{
int n;
vector<int> V, M( 2 );

cout << "Enter n: ";
cin >> n;

V.resize( n );
for( int i = 0; i < n; i++ )
V[i] = i;

random_shuffle( V.begin(), V.end() );

cout << "Using min and max separately" << endl;
cout << "Min = " << min( V ) << endl;
cout << "Max = " << max( V ) << endl;

cout << "Using minmax" << endl;
M = minmax( V,0,n-1 );
cout << "Min = " << M[0] << endl;
cout << "Max = " << M[1] << endl;

return 0;
}
Topic archived. No new replies allowed.