Back

Jump Search

Intermediate
Search
Requires sorted array

Jump Search skips ahead in blocks of size √n to find the target's block, then does a linear scan backward. It bridges the gap between O(n) linear and O(log n) binary search — achieving O(√n).

Auto-sorted: Array is sorted ascending. Block size = √ — thin vertical lines mark block boundaries.

Search Target

Quick examples:

↑ Set a target value above to begin searching the array

Loading controls...
Loading visualization...
Time: O(√n)
Space: O(1)

Legend

Jump Check (J)
Last element of block — compared to target
Linear Scan (→)
Element being checked in Phase 2
Scanned
Checked in linear scan, no match
Found!
Target located here
Not Found
All elements checked, no match
Eliminated
Blocks already jumped past
— — — target: N
Dashed line = target height
Pointer labels:
BBlock start (first index of current block)
JJump check (last index of current block)
EBlock end (during linear scan)
Linear scan cursor
Found position
Block size = √n ≈ 4
Thin lines mark block boundaries

Inspector

No active frame to inspect

Reference implementation of Jump Search. Select a language tab to view and copy the code.

import math

def jump_search(arr: list[int], target: int) -> int:
    n    = len(arr)
    step = int(math.sqrt(n))
    prev = 0

    # Phase 1: jump over blocks
    while arr[min(step, n) - 1] < target:
        prev = step
        step += int(math.sqrt(n))
        if prev >= n:
            return -1

    # Phase 2: linear search within block
    while arr[prev] < target:
        prev += 1
        if prev == min(step, n):
            return -1

    return prev if arr[prev] == target else -1

Python — Code Walkthrough

  1. import math for sqrt

    import math; step = int(math.sqrt(n))

    math.sqrt returns a float; int() truncates it to ⌊√n⌋. Python does not overflow, but using int(math.sqrt(n)) is safer and clearer than n ** 0.5.

  2. Phase 1 comment — blocks

    # Phase 1: jump over blocks

    While the last element of the current block is less than target, the entire block can be skipped. min(step, n) prevents index out-of-range when n is not a perfect square.

  3. Phase 2 comment — linear scan

    # Phase 2: linear search within block

    Once the jump loop stops, we know target (if present) is between prev and min(step, n)−1. A simple forward walk checks each element.

  4. Conditional return

    return prev if arr[prev] == target else -1

    Python's conditional expression (ternary) reads naturally. After the linear scan arr[prev] is the first element ≥ target; we return prev only if it's an exact match.