1 Replies - 174 Views - Last Post: 12 October 2012 - 11:24 PM Rate Topic: -----

#1 nikspats  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1628
  • View blog
  • Posts: 3,087
  • 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/
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1