How jump search blocks a sorted array

Short answer: Jump search walks a sorted array in strides of length m and, once a stride overshoots the target, scans the previous block linearly. The cost is about n/m + m comparisons. That sum is minimized at m = √n, which makes the search Θ(√n). On random-access memory, binary search’s log n is smaller. The block walk is the right shape when jumping backward is the expensive move.


What the problem actually asks

Suppose moving forward through the array is cheap and a backward seek is costly, as on a tape, or suppose a comparison is so cheap that the interesting cost is the number of blocks touched. Fixed strides use the sorted order without computing a midpoint. The stride length is a trade: a longer stride means fewer jumps and a longer linear cleanup.

The cleanup is necessary. The stride only finds a block that contains the lower bound. Items inside the block are not yet ordered relative to the probe in a way that would let you skip them, short of searching that block by a second method.


What the algorithm guarantees

With stride m, the jump phase reads at most ⌈n/m⌉ keys and the linear phase reads at most m−1 more. The sum ⌈n/m⌉ + m is Θ(√n) when m is Θ(√n), and the minimum over real m sits at √n. Extra memory is a few indices. Correctness is the sorted order: if a[i] < target and i is a stride boundary, the answer is strictly after i, so every earlier block is discarded.

Replacing the linear cleanup with a binary search inside the block gives a hybrid whose cost is one jump phase plus log m. That hybrid is no longer classical jump search, and on RAM it still loses to binary search over the whole array, which never spends the jump phase.


The working steps

  1. Choose m = floor(√n), and at least 1.
  2. Set i = 0. While i+m < n and a[i+m] < target, add m to i.
  3. The lower bound now lies in (i, min(i+m, n−1)], or at i if a[i] is already ≥ the target.
  4. Scan that block linearly, or binary-search it, and return the first index whose key is ≥ the target.
  5. If the scan passes the block with every key smaller, the target is absent and the lower bound is n.

The √n bound is a comparison count for a sorted array. It does not beat binary search on a machine where every index costs the same. It is the wrong tool for unsorted data, where no stride can be discarded.


Approaches that look simpler and fail

Approach Why it fails when the stride is arbitrary
Stride by a constant The jump count stays Θ(n) and the algorithm has become a slower linear search.
Stride by n/2 The cleanup scan is Θ(n). One big jump does not locate a small block.
Jump on an unsorted array a[i+m] < target says nothing about the items skipped between i and i+m.
Prefer it over binary search on RAM because √n looks simple log₂ n is smaller than √n for every array longer than a few dozen items. The tape argument is the one that keeps jump search.

Equal-cost random access belongs to binary search. A hit expected near the front belongs to exponential search, whose cost tracks the position rather than √n.


A working example

The Bash script and the Python script jump-search with block length floor(sqrt(n)). Both print the index 6.

a=(0 1 1 2 3 5 8 13 21)
target=8
n=${#a[@]}
step=1
while (( step * step <= n )); do step=$((step+1)); done
step=$((step-1))
if (( step < 1 )); then step=1; fi
prev=0
cur=$step
while (( a[cur < n ? cur : n-1] < target )); do
  prev=$cur
  cur=$((cur+step))
  if (( prev >= n )); then echo missing; exit 1; fi
done
if (( cur > n )); then cur=$n; fi
idx=$prev
while (( idx < cur && a[idx] < target )); do idx=$((idx+1)); done
echo "$idx"
a = [0, 1, 1, 2, 3, 5, 8, 13, 21]
target = 8
n = len(a)
step = int(n ** 0.5)
prev = 0
while a[min(step, n) - 1] < target:
    prev = step
    step += int(n ** 0.5)
    if prev >= n:
        raise SystemExit("missing")
idx = prev
while idx < min(step, n) and a[idx] < target:
    idx += 1
assert a[idx] == target
print(idx)

Where it is used

Jump search scans a sorted array in fixed strides, which was a reasonable pattern when a sequential skip was cheaper than a random probe. Binary search replaced it for arrays in RAM. The stride idea remains in any blocked scan of a sorted file.


When you can drop this algorithm

Drop jump search on ordinary RAM. Binary search does fewer comparisons and has a simpler invariant. Keep it when forward motion is the natural access, backward motion is expensive, and a block of length √n is an acceptable linear cleanup.


Why this stays published

We publish this so a search for jump search returns the √n stride and the reason binary search replaces it on random-access memory. Jump search strides by √n through a sorted array and scans one block, for Θ(√n) comparisons, which random-access machines replace with binary search.

本文由 HoHo 與 AI 協作整理,最後更新於 2026 年 11 月 13 日。

Leave a Reply

Your email address will not be published. Required fields are marked *