# need help to find # comparison

Page 1 of 1

## 1 Replies - 213 Views - Last Post: 12 October 2012 - 11:24 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=295374&amp;s=6b213a827fffd672f873fbc92ed38129&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 nikspats

Reputation: 0
• Posts: 1
• Joined: 12-October 12

# 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[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;
}

```

Is This A Good Question/Topic? 0

## Replies To: need help to find # comparison

### #2 Salem_c

• void main'ers are DOOMED

Reputation: 1889
• Posts: 3,695
• 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/