#include
/**
*
* Exercise 3-1. Write a version of the following binary
* search with only one test inside the loop and measure
* the difference in run-time.
*
* int binsearch(int x, int v[], int n)
* {
* int low, high, mid;
*
* low = 0;
* high = n - 1;
* while (low <= high) {
* mid = (low+high) / 2;
* if (x < v[mid])
* high = mid - 1;
* else if (x > v[mid])
* low = mid + 1;
* else
* return mid;
* }
* return -1;
* }
*/
int binsearch(int x, int v[], int n)
{
int low, high, mid;
low = 0;
high = n-1;
mid = (low+high)/2;
while(x != v[mid] && low <= high) {
if( x < v[mid] )
high = mid - 1;
else
low = mid + 1;
mid = (low+high)/2;
}
return (x==v[mid]) ? mid : -1;
}