Check the "middle" data if that is what you are looking for, return else if the middle data is smaller than what you are looking for, search the top half else search the bottom half.
low high mid 0 1 2 3 4 5 0 5 8 9 10 15
BinarySearch(A, low, high, key)
- found ← false
- while low ≤ high and not found
- mid ← ( low + high) / 2
- if A[mid] = key
- found ← true
- else if A[mid] < key
- low ← mid+1
- else
- high ← mid-1
- if not found
- mid ← high+1
- return mid