Jump Search is the pragmatic middle child between Linear and Binary Search. It doesn’t scan every element, nor does it need binary search’s midpoint machinery.
Instead, it jumps forward in fixed-size blocks until it overshoots the target, then does a small linear scan inside the block that must contain it.
Think of reading a book by flipping ahead a chapter at a time, then scanning that chapter page by page.
How It Works
- Pick a block size — the optimal is
⌊√n⌋. - Jump forward by one block while the block’s end element is still smaller than the target.
- When a block’s end exceeds (or equals) the target, the target — if present — is inside it.
- Linearly scan that block; return the index if found, otherwise -1.
Key Insight
The √n block size isn’t arbitrary — it’s the answer to a small optimization problem.
- With block size
b, jumps cost aboutn/band the final linear scan costs up tob. - Total work ≈
n/b + b, minimized whenb = √n→O(√n).
That’s a genuine improvement over O(n) linear search. Because it only walks forward, it also has better cache locality than binary search’s non-contiguous midpoints on some hardware.
Worked Example
Search for 10 in [1, 3, 5, 7, 9, 11, 13, 15, 17, 19] with block size ⌊√10⌋ = 3:
- Jump to index 2:
arr[2] = 5 < 10→ continue - Jump to index 5:
arr[5] = 11 ≥ 10→ target must be in indices 3–5 ([7, 9, 11]) - Linear scan indices 3, 4, 5:
7, 9, then11 ≠ 10→ not in this block → return -1
Search for 9 instead: linear scan hits index 4 → return 4. In the visualizer, the cursor leaps by the block size, then creeps forward one position at a time within the final block.
Edge Cases & Pitfalls
- Empty array — return -1 without jumping.
- Target at the very start — caught on the first linear scan: best case
O(1). - Target beyond the last block — the final jump clamps to the array end; the last partial block gets scanned.
- Block size zero — with
n = 0, guard against√0producing a degenerate block. - Not a substitute for binary search —
O(√n)is far worse thanO(log n)on huge sorted arrays. Jump search wins only when jumps are cheaper than midpoint comparisons.
Comparison With Other Searches
| Scenario | Jump Search | Linear Search | Binary Search |
|---|---|---|---|
| Requires sorted input | Yes | No | Yes |
| Average time | O(√n) | O(n) | O(log n) |
| Cache behavior | Sequential-friendly | Sequential | Random jumps |
| Best when | Sorted + cheap sequential reads | Unsorted / tiny | Large sorted data |
Applications
- Sorted data where the storage favors sequential access over random access (e.g., certain disk/tape layouts)
- A simple, low-memory improvement over linear search with no recursion or midpoint arithmetic
- Educational stepping stone between linear and binary search
Practice Trajectory
- Hand-trace Jump Search for target 12 in
[2, 4, 6, 8, 10, 12, 14, 16]with block size 2 and with block size 3. - Derive why
n/b + bis minimized atb = √n. - Explain what happens when the target is larger than every element.
- Compare the number of comparisons for n=100 across linear, jump, and binary search.
- Implement Jump Search and handle an empty array gracefully.