ArticlesSearch and sort
Binary search
On sorted data, compare with the middle and throw away half. Keep an invariant for where the target can still sit.
If an array is sorted, you do not have to look at every index. Compare the target with the middle value. If the target is smaller, it cannot live in the right half. If it is larger, it cannot live in the left half. Each comparison throws away half of what remains. That is O(log n) comparisons.
The bugs are off by one. Keep an invariant you can say out loud: if the target is present, its index is between lo and hi inclusive. When the middle value is too small, the answer is at least one past the middle. When it is too large, the answer is at most one before the middle. When the range is empty, the target is absent. Return -1.
The array must be sorted. Binary search on unsorted data is not "usually fine". It is wrong, because the half you throw away might have held the value. Compute the middle as lo plus half the distance to hi, so the index stays inside the range you still trust.