A[i] ≤ A[i+1] for all i between 0 and size-2.
A, a size s, and an item key, determine if key is present in A.
set position to 0
set found to false
while position < size and not found
if A[position] = key
found = true
else
position = position + 1
return found (alternative, return position)
| Index | Data |
|---|---|
| 0 | dog |
| 1 | fox |
| 2 | ant |
| 3 | cat |
| 4 | rat |
| Index | Data |
|---|---|
| 0 | ant |
| 1 | cat |
| 2 | dog |
| 3 | fox |
| 4 | rat |
A, a size s, and an item key, determine if key is present in A.
set first to 0
set last to size-1
set found to false
while first ≤ last and not found
mid = (first + last) /2
if A[mid] == key
found = true
else if A[mid] < key
first = mid+1
else
last = mid-1