Range Finding Index   <<   >>


 
Find the range with 2×n comparisons

min=max=*x++;
for(i=1; i<n; ++i,++x)
{
  if(min>*x)min=*x;
  if(max<*x)max=*x;
}

  Find the range with 1.5×n comparisons

min=max=*x++; --n; m=n/2;
for(i=0; i<m; ++i,x+=2)
{
  if(x[0]<x[1]){if(min>x[0])min=x[0]; if(max<x[1])max=x[1];}
  else         {if(min>x[1])min=x[1]; if(max<x[0])max=x[0];}
}
if(n%2){if(min>*x)min=*x; if(max<*x)max=*x;}

Which is faster on modern machines?


[Spoiler Alert!]














The standard coding is faster by a factor of 2.