> For the complete documentation index, see [llms.txt](https://nielslange.gitbook.io/developer-toolbox/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://nielslange.gitbook.io/developer-toolbox/algorithms/search-algorithms/jump-search.md).

# Jump Search

Jump search is an algorithm for searching for a target value in a sorted array. It's similar to linear search in that it looks at every nth element, but it skips over some elements in a way that makes it more efficient.

### Example in TypeScript

```typescript
function jumpSearch(arr: number[], target: number): number | null {
    const n = arr.length;
    let step = Math.floor(Math.sqrt(n));
    let prev = 0;
    while (arr[Math.min(step, n) - 1] < target) {
        prev = step;
        step += Math.floor(Math.sqrt(n));
        if (prev >= n) {
            return null;
        }
    }
    while (arr[prev] < target) {
        prev++;
        if (prev === Math.min(step, n)) {
            return null;
        }
    }
    if (arr[prev] === target) {
        return prev;
    }
    return null;
}
```

This function takes two arguments: an array and a target value. It uses a while loop to jump over some elements in the array by taking `sqrt(n)` steps at a time. It starts with the first element and compares it with the target value. If the element is less than the target value, it moves to the next step. It keeps moving to the next step until it finds an element that is greater than or equal to the target value. Then it uses another while loop to check from the previous step until it finds the target value or reaches the end of the array. If it finds the target value, it returns the index. If it doesn't find the target value, it returns `null`.

#### Usage

```typescript
const array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log(jumpSearch(array, 4)); // Output: 3
console.log(jumpSearch(array, 10)); // Output: null
```

Jump search is an O(√n) algorithm, which is faster than [linear search](/developer-toolbox/algorithms/search-algorithms/linear-search.md) but slower than [binary search](/developer-toolbox/algorithms/search-algorithms/binary-search.md). It is useful when the size of the array is too large to be able to scan every element using linear search, but also when the array is not big enough to justify the use of more complex algorithms like binary search or [interpolation search](/developer-toolbox/algorithms/search-algorithms/interpolation-search.md). This algorithm works only with sorted arrays.


---

# 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:

```
GET https://nielslange.gitbook.io/developer-toolbox/algorithms/search-algorithms/jump-search.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
