# need help to find # comparison

Posted 12 October 2012 - 01:22 PM

need little help. I just want to count the number of comparisons used when called with a vector of size n. I do have code ready. need some change

```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 = V[l]; M = V[u];
return M;
}

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

m =( u+l )/2;

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

if( TRACING )
{
cout << "M1 = " << M1 << "," << M1 << endl;
cout << "M2 = " << M2 << "," << M2 << endl;
}

M = min( M1,M2 );
M = max( M1,M2 );
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 << endl;
cout << "Max = " << M << endl;

return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: need help to find # comparison

### #2 Salem_c Reputation: 2408
• Posts: 4,542
• Joined: 30-May 10

## Re: need help to find # comparison

Posted 12 October 2012 - 11:24 PM

Looks like the same code.
http://www.dreaminco...racing-to-work/

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }