Jump Search
On sorted data, check only the last value of each block until one is not smaller than the target, then step back and scan that one block. One backward move, about 2√n comparisons.
How Jump Search Works
- Set the block size m = ⌊√n⌋.
- Read a[m − 1], a[2m − 1], … until a value is ≥ the target or the array ends.
- Step back to the first index of that block.
- Scan forward. Return the index of a match; stop with -1 at the first larger value or the block's end.
Jump search works on a sorted array. It divides the array into blocks of m values and reads only the last value of each block. While that value is smaller than the target, the whole block is smaller too, so it is skipped. The first block whose last value is at least the target is the only one that can hold it, and a short linear scan of that block finishes the job.
On 3, 7, 12, 18, 21, 27, 33, 38, 42, 47, 53, 58, 64, 69, 74, 80 with m = 4, finding 69 reads 18, 38 and 58 (all smaller), then 80 (not smaller). The target must be in the last block, so the scan reads 64 and then 69: six comparisons. Linear search needs 14 on the same array and binary search needs 3.
- Sorted order is what lets one read rule out a whole block
- The scan stops early at the first value larger than the target
- If every block end is smaller than the target, it is absent
Choosing the Block Size
The block size trades jumps against the scan. In the worst case jump search makes about n/m jumps and then up to m steps inside the last block, so the cost is about n/m + m.
Small blocks mean many jumps, large blocks mean a long scan. The sum is smallest when the two parts are equal, n/m = m, which gives the optimal jump size m = √n and a worst case of about 2√n. For 69 on the 16-value array, blocks of 2 cost 9 comparisons, blocks of 8 cost 8, and blocks of 4 cost 6.
- Worst case ≈ n/m + m, minimised at m = √n
- Round √n down; the last block may be shorter than the rest
- A different m is still correct, just slower
Jump Search Time Complexity
With m = √n there are at most √n jumps and at most √n − 1 scan steps, so jump search takes O(√n) time in the worst and average case. The best case is O(1), when the first block end is the target. It keeps two indexes, so the extra space is O(1).
√n sits between the two classic searches. For a million values linear search can need 1,000,000 comparisons, jump search about 2,000 and binary search about 20.
| Values | Linear O(n) | Jump O(√n) | Binary O(log n) |
|---|---|---|---|
| 16 | 16 | 8 | 5 |
| 10,000 | 10,000 | 200 | 14 |
| 1,000,000 | 1,000,000 | 2,000 | 20 |
- Best O(1), worst and average O(√n), space O(1)
- Always more comparisons than binary search on large arrays
Terms, operations, and practical uses
Key terms
- Block size mHow far each jump moves; √n gives the smallest worst case.
- Block endThe last value of a block, the only one read during the jumping phase.
- Sparse indexOne index entry per block of a sorted file, scanned like jump search.
Related
- Linear searchThe scan jump search uses inside the final block.
- Binary searchO(log n) on the same sorted arrays.
- Skip listsExpress lanes over a sorted linked list, a layered form of the same idea.
- Search Insert PositionFind where a value belongs in a sorted array.
Code in Python, C++ and Java
import math
def jump_search(a, target, m):
n = len(a)
checks = 0
prev, end = 0, m
while True:
checks += 1
if a[min(end, n) - 1] >= target:
break
prev, end = end, end + m
if prev >= n:
return -1, checks
for i in range(prev, min(end, n)):
checks += 1
if a[i] >= target:
return (i if a[i] == target else -1), checks
return -1, checks
nums = [3, 7, 12, 18, 21, 27, 33, 38, 42, 47, 53, 58, 64, 69, 74, 80]
m = math.isqrt(len(nums))
for target in (69, 66, 80):
i, checks = jump_search(nums, target, m)
print(f"block {m}, find {target}: index {i}, {checks} comparisons")
for size in (2, 8):
i, checks = jump_search(nums, 69, size)
print(f"block {size}, find 69: index {i}, {checks} comparisons")#include <algorithm>
#include <cmath>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;
pair<int, int> jumpSearch(const vector<int>& a, int target, int m) {
int n = a.size(), checks = 0, prev = 0, end = m;
while (true) {
checks++;
if (a[min(end, n) - 1] >= target) break;
prev = end;
end += m;
if (prev >= n) return {-1, checks};
}
for (int i = prev; i < min(end, n); i++) {
checks++;
if (a[i] >= target) return {a[i] == target ? i : -1, checks};
}
return {-1, checks};
}
int main() {
vector<int> nums = {3, 7, 12, 18, 21, 27, 33, 38, 42, 47, 53, 58, 64, 69, 74, 80};
int m = (int)sqrt((double)nums.size());
for (int target : {69, 66, 80}) {
auto [i, checks] = jumpSearch(nums, target, m);
cout << "block " << m << ", find " << target << ": index " << i << ", " << checks << " comparisons\n";
}
for (int size : {2, 8}) {
auto [i, checks] = jumpSearch(nums, 69, size);
cout << "block " << size << ", find 69: index " << i << ", " << checks << " comparisons\n";
}
}public class Main {
static int[] jumpSearch(int[] a, int target, int m) {
int n = a.length, checks = 0, prev = 0, end = m;
while (true) {
checks++;
if (a[Math.min(end, n) - 1] >= target) break;
prev = end;
end += m;
if (prev >= n) return new int[]{-1, checks};
}
for (int i = prev; i < Math.min(end, n); i++) {
checks++;
if (a[i] >= target) return new int[]{a[i] == target ? i : -1, checks};
}
return new int[]{-1, checks};
}
public static void main(String[] args) {
int[] nums = {3, 7, 12, 18, 21, 27, 33, 38, 42, 47, 53, 58, 64, 69, 74, 80};
int m = (int) Math.sqrt(nums.length);
for (int target : new int[]{69, 66, 80}) {
int[] r = jumpSearch(nums, target, m);
System.out.println("block " + m + ", find " + target + ": index " + r[0] + ", " + r[1] + " comparisons");
}
for (int size : new int[]{2, 8}) {
int[] r = jumpSearch(nums, 69, size);
System.out.println("block " + size + ", find 69: index " + r[0] + ", " + r[1] + " comparisons");
}
}
}Step through it
Running on [3, 7, 12, 18, 21, 27, 33, 38, 42, 47, 53, 58, 64, 69, 74, 80] · target 69 · block 4
Where Jump Search Fits
On an array in memory binary search is faster, so why do data structure courses still teach jump search? Because of how it moves. Binary search jumps back and forth across the array; jump search only moves forward, except for one step back to the start of the final block.
That matters when going backwards is expensive: data on tape, a file read through a forward-only cursor, or a sorted linked structure with forward skip pointers every √n nodes. The same idea, a coarse pass followed by a fine pass, appears in skip lists and in the sparse index of a database file, which keeps one entry per block and scans inside the block.
- One backward move, against binary search's many
- Coarse pass then fine pass: the idea behind sparse indexes and skip lists
- On plain arrays in RAM, prefer binary search
Edge Cases
If n is not a perfect square, the last block is shorter; clamp the jump to the last index so the read never goes past the end. If the target is smaller than a[0], the first block end is already larger, and the scan stops at index 0 with -1. With duplicates the scan returns the first copy inside the block it reaches, which is the first copy in the array, because every earlier block ended below the target.
- Clamp each jump with min(index, n − 1)
- Duplicates: the first occurrence is returned
- Unsorted input gives wrong answers, not slow ones