Lesson 8 · Core algorithms

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.

Jump Search concept diagramSixteen sorted values in four blocks of four. Searching for 69, jump search checks the last value of each block in turn: 18, 38, 58, then 80, which is at least 69. It steps back once to 64, the start of the last block, and walks forward to 69: six comparisons. Below, a curve of the worst-case cost n/m + m for block sizes 1 to 16 bottoms out at block size 4, the square root of 16, with 8 comparisons.find 69: jump to each block's end, then walk one blockblock 1block 2block 3371218#121273338#242475358#364#569#67480#418 < 6938 < 6958 < 69 · then 80 ≥ 69block 4: one step backm = √16 = 4 → 814816block size mworst case n/m + m: 17 at m = 117 at m = 16
1

How Jump Search Works

  1. Set the block size m = ⌊√n⌋.
  2. Read a[m − 1], a[2m − 1], … until a value is ≥ the target or the array ends.
  3. Step back to the first index of that block.
  4. 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
2

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
3

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.

Worst-case comparisons on sorted data
ValuesLinear O(n)Jump O(√n)Binary O(log n)
161685
10,00010,00020014
1,000,0001,000,0002,00020
  • Best O(1), worst and average O(√n), space O(1)
  • Always more comparisons than binary search on large arrays
Key reference

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.
Implementation

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");
        }
    }
}
Watch it run

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

target 69 · block size m = 4comparisons: 03071122183214275336387428479531058116412691374148015block 1block 2block 3block 4one read can rule out a whole block, because the data is sorted
Step 1

Four blocks of four

Jump search needs sorted data. It splits the 16 values into blocks of m = √16 = 4 and checks only the last value of each block. If that value is smaller than the target, the whole block is smaller and can be skipped without reading the other three.

Output
1 / 10
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
5

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