I've been trying to implement it all day. But can't seem to convert an Array based bit of code.
1 2 3 4 5 6 7 8 9 10 11 12 13
int binarySearch(int sortedArray[], int first, int last, int key) {
while (first <= last) {
int mid = (first + last) / 2; // compute mid point.
if (key > sortedArray[mid])
first = mid + 1; // repeat search in top half.
elseif (key < sortedArray[mid])
last = mid - 1; // repeat search in bottom half.
elsereturn mid; // found it. return position /////
}
return -(first + 1); // failed to find key
}