Adobe Question: Implement Binary Search using one comparison per iteration.

int binarySearch( int* a, int value, int low, int high, int size)
{
while (low < high)
{
int mid = low + ((high - low) / 2);
if (a[mid] < value)
low = mid + 1;
else
high = mid;
}
if ((low < size) && (a[low] = = value))
return low;
else
return -1;
}

Comments

Popular posts from this blog