Jump Search
Search a sorted array by jumping ahead in fixed blocks of size √n, then scanning linearly within the block.
You have not marked these as done yet. Read on anyway if they feel familiar; come back to them if something here does not land.
Overview
Jump search works on a sorted array by stepping forward in blocks of size m (optimally m = √n) until it finds a block whose last element is ≥ the target, then scanning that block linearly. It costs O(√n) comparisons — between Linear Search and Binary Search.
Its selling point is that it only ever moves forward, so it suits media where jumping backwards is expensive (tape, sequential storage, singly linked lists with a skip index) and it makes fewer far jumps than binary search.
Intuition
A mental model before the formal terms.
Looking for page 347 in a book with no index: flip ahead 20 pages at a time until you pass 347, then step back and turn single pages. With 400 pages you flip at most 20 big jumps plus 20 single pages, not 400.
How it works
- Choose the block size
m = ⌊√n⌋. - Set
prev = 0,step = m. Whilea[min(step, n) - 1] < target:prev = step,step += m. Ifprev >= n, the target is absent. - Linear-scan from
prevwhilea[prev] < target; stop when reachingmin(step, n). - If
a[prev] == target, returnprev; otherwise return-1.
Why it works
Because the array is sorted, the target can only lie in the first block whose last element is ≥ the target — every earlier block ends below it.
At most n/m jumps plus at most m - 1 linear steps; minimizing n/m + m gives m = √n and a total of 2√n comparisons.
Recognition
How to tell a problem wants this.
- Sorted data on a medium where backward seeks are costly or forbidden.
- The problem asks for something better than linear but you cannot use random access freely (e.g. an indexed sequential file).
- Rarely the intended answer in interviews — it appears mostly as a discussion of the √n decomposition idea.
Interactive visualization
Play, step, change the input. ← → and space work too.
1step = floor(sqrt(n)); prev = 02while a[min(step, n) - 1] < target:3 prev = step; step += floor(sqrt(n))4 if prev >= n: return -15for i in prev .. min(step, n) - 1:6 if a[i] == target: return i7return -1Pseudocode
1m = floor(sqrt(n)); prev = 0; step = m2while a[min(step, n) - 1] < target:3 prev = step; step += m4 if prev >= n: return -15while a[prev] < target:6 prev += 17 if prev == min(step, n): return -18if a[prev] == target: return prev9return -1Implementations
1import math2from typing import Sequence3 4 51 · Step forward in blocks of sqrt(n) until the block could contain target6def jump_search(a: Sequence[int], target: int) -> int:7 n = len(a)8 if n == 0:9 return -110 step = max(1, math.isqrt(n))11 12 prev, curr = 0, step13 while prev < n and a[min(curr, n) - 1] < target:14 prev = curr15 curr += step16 if prev >= n:17 return -1 # ran off the end without reaching target18 192 · Scan the identified block linearly; it holds at most step elements20 end = min(curr, n)21 for i in range(prev, end):22 if a[i] == target:23 return i24 if a[i] > target:25 break # sorted, so no later element can match26 return -127 28 293 · Why sqrt(n): n/step jumps plus step scans, minimised at step = sqrt(n)30def worst_case_probes(n: int, step: int) -> int:31 return n // step + step # minimal at step = sqrt(n), giving 2*sqrt(n)32 33 344 · On a random-access list binary search dominates; jump search is for35# sequential sources where seeking backward is expensive or impossible36def block_count_for(n: int) -> int:37 return math.ceil(n / math.sqrt(n))math.isqrt(n)is the exact integer square root — no float round-trip, no rounding error, available since Python 3.8.prev, curr = 0, stepuses tuple assignment for the two cursors, matching how the loop advances them together.a[min(curr, n) - 1]clamps the block-end probe exactly as in the other languages.range(prev, end)is half-open, which lines up withend = min(curr, n)being one past the last index to scan.- The
breakona[i] > targetexploits sortedness to cut the average block scan in half.
bisect is O(log n) and runs in C, so on a real list it beats this by a wide margin — jump search is for sequential sources.
math.isqrtreturns the floor of the exact square root for arbitrarily large integers, unlikeint(math.sqrt(n))which loses precision past 2^53.math.sqrtreturns a float and raisesValueErroron negatives;isqrtraises on negatives too but never rounds.- For a genuinely sequential source,
itertools.isliceexpresses "skip step items" without materialising them. bisect_leftis the right tool for any in-memory sorted list; this algorithm earns its place only when random access is expensive.
- Using
int(math.sqrt(n))on a hugen, where float rounding can produce a step one too large and skip the target block. - Writing
range(prev, end + 1), which reads one element past the block and, on the last block, past the list. - Forgetting the
if prev >= n: return -1guard, so a target above the maximum loops untilmin(curr, n) - 1stops moving.
- Integer square root: Python has exact
math.isqrt; C++, JavaScript and TypeScript go through adoublesqrtand floor it, which is exact only below 2^53. - Clamping: C++
std::minrequires matching types and needs explicit casts, whileMath.minand Pythonminaccept anything and coerce or compare directly. - The practical verdict is the same everywhere —
std::lower_bound,bisect_left, or a hand-written binary search beats this on any random-access container. - Where it does win differs by ecosystem: paged network fetches in JS/TS,
std::forward_listor memory-mapped tape in C++, and generator pipelines withitertools.islicein Python.
Complexity
At most 2√n comparisons with block size √n.
Compare growth rates in the Complexity Explorer →When to use — and when not to
- Sorted data where jumping backwards is expensive — sequential-access storage, tapes, one-directional cursors.
- When the number of far jumps matters more than total comparisons (each jump is a costly seek).
- As an intermediate technique to introduce √n decomposition.
- Ordinary in-memory sorted arrays — Binary Search is
O(log n)and just as simple. - Unsorted data; the block test relies on sortedness.
- Unbounded or unknown-length inputs — use Exponential Search.
Alternatives
Common mistakes
- Indexing
a[step - 1]past the end on the last block — always clamp withmin(step, n). - Using block size
n/2or a constant instead of√n, losing theO(√n)bound. - Not handling the empty array or a target larger than every element.
Interview patterns
- Explain the
n/m + mtrade-off and derivem = √n— a warm-up for √n decomposition and Mo's algorithm. - Compare seek counts: jump search makes ≤ √n forward seeks, binary search makes log n seeks that alternate direction.
- Recognizing the approach from an array and a targetIntermediate
- Deciding whether O(n²) can be improvedIntermediate
- Where does O(n log n) come from?Beginner
- Questions to ask before binary searchingIntermediate
- Minimum Size Subarray SumIntermediate
- Search in Rotated Sorted ArrayIntermediate
Example problems
No linked problems yet.
Check yourself
Answer out loud or on paper first, then compare. These come from the interview questions linked to this lesson.
Given an array and a target, how would you determine which algorithmic approach to use?
Show a strong answerHide the answer
A strong candidate does not answer with an algorithm. They answer with questions, and each question maps to a technique. Is the array sorted? Sorted plus a target means Binary Search for one element (
O(log n)) or Two Pointers (Opposite Ends) for a pair (O(n)). Do I need a pair, a subarray, or a subsequence? A pair with arbitrary order is a Hash Map problem (O(n)time,O(n)space). A contiguous subarray with a sum target is Prefix Sum plus a hash map of seen prefixes. A subsequence usually means Dynamic Programming or backtracking.Are the values constrained? All positive means a sliding window can grow and shrink monotonically (Sliding Window (Variable Size)); negatives break that monotonicity and force prefix sums. Small integer ranges (say
0..10^5) allow counting arrays instead of hash maps. What size is `n`?n ≤ 20invites2^nenumeration;n ≤ 5000toleratesO(n^2);n ≤ 10^5demandsO(n log n)or better.They then say the brute force out loud — usually
Full interview question, with follow-ups and red flags →O(n^2)over pairs orO(n^2)over subarrays — and identify what the inner loop is *looking for*. That inner lookup is the thing a hash map, a sorted order, or a running window replaces. This is the general engine: name the repeated question the brute force asks, then pick the structure that answers it inO(1)orO(log n).Your first solution is O(n²). How would you determine whether it can be improved?
Show a strong answerHide the answer
A strong candidate starts by asking two things: what is the lower bound and where is the redundant work. The lower bound is usually output size or the need to read the input once — if the problem asks for all pairs,
O(n^2)is optimal and the discussion is over. Otherwise, at leastO(n)is needed to read the input, so the gap betweennandn^2is the room for improvement.Then they open the inner loop and name what it does. Nearly every
O(n^2)inner loop is one of a few things: searching for a value (replace with a Hash Map or, on sorted data, Binary Search), recomputing a sum or count over a range (replace with Prefix Sum or a maintained running window), looking for the nearest greater/smaller element (replace with a Monotonic Stack), or re-solving an overlapping subproblem (replace with Memoization (Top-Down DP)). Each replacement turns anO(n)inner step intoO(1)orO(log n).They also check whether order can be exploited: sorting costs
O(n log n)once and often enables two pointers or binary search afterward. And they check for wasted recomputation between adjacent iterations — if iterationi+1recomputes most of what iterationicomputed, a sliding window or incremental update is available.Finally they verify the new complexity honestly, including the cost of the new structure (hash map space, sort time), and confirm that the constraint (
Full interview question, with follow-ups and red flags →n ≤ 10^5implies roughly10^5 log 10^5 ≈ 1.7 × 10^6operations) is actually satisfied.The array is sorted and I need two elements summing to the target. Hash map or two pointers?
Show a strong answerHide the answer
Two pointers: same
Full interview question, with follow-ups and red flags →O(n)time butO(1)space and no hashing overhead. A hash map only wins if the array is unsorted and sorting is not allowed or indices must be preserved.