> For the complete documentation index, see [llms.txt](https://stephanosterburg.gitbook.io/scrapbook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://stephanosterburg.gitbook.io/scrapbook/math/algorithm-khan-academy/binary-search.md).

# Binary Search

#### Pseudocode

Here's the pseudocode for binary search, modified for searching in an array. The inputs are the array, which we call `array`; the number `n` of elements in `array`; and `target`, the number being searched for. The output is the index in `array` of `target`:

Here is modified pseudocode for binary search that handles the case in which the target number is not present:

1. Let `min = 0` and `max = n-1`.
2. If `max < min`, then stop: `target` is not present in `array`. Return `-1`.
3. Compute `guess` as the average of `max` and `min`, rounded down (so that it is an integer).
4. If `array[guess]` equals `target`, then stop. You found it! Return `guess`.
5. If the guess was too low, that is, `array[guess] < target`, then set `min = guess + 1`.
6. Otherwise, the guess was too high. Set `max = guess - 1`.
7. Go back to step 2.

```python
import math

primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]


def doSearch(array, target_value):
    min_value = 0
    max_value = len(array) - 1

    while min_value <= max_value:
        guess = int(math.floor((max_value + min_value) / 2))
        if array[guess] == target_value:
            return guess
        elif array[guess] < target_value:
            min_value = guess + 1
        else:
            max_value = guess - 1

    return -1;


target = 73
print(doSearch(primes, target))
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://stephanosterburg.gitbook.io/scrapbook/math/algorithm-khan-academy/binary-search.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
