Jump search
how it works
The idea, two pictures, then one search step by step.
Jump search needs a row in order, smallest first. It splits the row into blocks, each the square root of long, rounded down, called : 9 numbers make blocks of 3. It looks at each block's last number in turn. A block whose last number is smaller than the number wanted is passed whole. When a last number is not smaller, every block before ends below the number wanted, as the passed blocks in the picture do. So the number, if it is there, first stands in this block. The search walks through it from its start, to the number wanted or a bigger one. If the walk gets to the block's last number, reading it again is one more look, 44 in the picture.
Looking for 6: 2 looks, found at place 1.
Looking for 44: 5 looks, found at place 6.
Looking for 40: 5 looks, not there.
Looking for 80: 3 looks, not there.
- a look, numbered in turn
- found, ringed
- looked at, not it
- ruled out without a look
- never reached
- a block passed whole
- the block looked through
- a block never reached
Looking for 51: 3 looks, found at place 2.
- a look, numbered in turn
- found, ringed
- looked at, not it
- a block passed whole
- the block looked through
step 1 of 12
There are 9 numbers in a row, in order, smallest first. Look for 42, jumping 3 numbers at a time.
what it remembers to know where it is
- looking for
- 42
- block
- not yet
- looking at place
- not yet
Where it says not yet, nothing is held there at that moment.
step 2 of 12
Look at the last number of block 1, at place 3. It is 18.
what it remembers to know where it is
- looking for
- 42
- block
- 1
- looking at place
- 3
step 3 of 12
18 is smaller than 42, and nothing before it is bigger, so rule out the whole block, places 1 to 3.
what it remembers to know where it is
- looking for
- 42
- block
- 1
- looking at place
- 3
step 4 of 12
Look at the last number of block 2, at place 6. It is 42.
what it remembers to know where it is
- looking for
- 42
- block
- 2
- looking at place
- 6
step 5 of 12
42 is not smaller than 42, so the first place 42 could stand is in this block. Look through it from place 4, as an earlier place in it may hold 42 too.
what it remembers to know where it is
- looking for
- 42
- block
- 2
- looking at place
- 6
step 6 of 12
Look at place 4. It holds 23.
what it remembers to know where it is
- looking for
- 42
- block
- 2
- looking at place
- 4
step 10 of 12
Look at place 6. It holds 42.
what it remembers to know where it is
- looking for
- 42
- block
- 2
- looking at place
- 6
step 11 of 12
42 is the number wanted, at place 6, so it is found.
what it remembers to know where it is
- looking for
- 42
- block
- 2
- looking at place
- 6
the steps, in plain words
choose a step to mark the Python, a programming language, that does it
- split the row into blocks of about √n numbersPython line 11
- look at the last number of the next blockPython lines 12, 13 and 14
- if it is smaller than the number wanted, rule out the whole blockPython lines 14 and 15
- otherwise look through this block from its startPython line 16
- if a number is the number wanted, it is foundPython lines 17 and 18
- if a number is bigger, the number wanted is not in the rowPython lines 19 and 20
- past the last block, it is not in the rowPython line 21
the same in Python
The steps in plain words are what to learn. Python is a programming language, and the code below does the same steps, for those who code. Python numbers the places in a list from 0, not 1: the first number is numbers[0], the second numbers[1], and so on.
import math
def jump_search(row, target):
"""Find target in a row in order, smallest first, a block at a time.
Returns its place, counted from 0 as Python counts places, or None
when target is not in the row. The blocks hold the square root of
the row's length, rounded down, numbers each.
"""
size = max(1, math.isqrt(len(row))) # the block: the root, rounded down
for start in range(0, len(row), size): # each block in turn, from the left
end = min(start + size, len(row)) - 1 # the block's last place
if row[end] < target: # one look: is its last number smaller?
continue # then target is past this whole block
for place in range(start, end + 1): # look through this block
if row[place] == target: # one look: is this the number wanted?
return place # found, at the first place it stands
if row[place] > target: # the same look: is it bigger?
return None # then it is not in the row
return None # past the last block: it is not in the row
how it scales
How the looks grow, from ten numbers to a million.
Each step across or up is ten times the one before. Standing first always costs 2 looks, a flat line. The most is 20 at 100, 63 at 1,000, 200 at 10,000: about 3.16 times for each ten times the numbers, and 3.16 is the root of 10.
looks, ten times more at each mark up
, how many numbers are in the row, ten times more at each mark to the right
- the last number of the last whole blocksolid where measured by running it; dashed where its formula carries it on to ten millionslope 0.5: ten times n, about 3 times the work
- a number in the row, no two equal, each place as likelypoints only, each measured by a real run, since no single formula for the looks of this row is worked out here
- the number wanted standing firstsolid where measured by running it; dashed where its formula carries it on to ten million
| looks | time | |
|---|---|---|
| 1,000 | 63 | about 63 nanoseconds |
| 100,000 | 632 | about 632 nanoseconds |
| 10,000,000 | 6,324 | about 6.3 microseconds |
The looks are the same on any computer. The times are not: a real computer may be faster or slower than a billion a second.
make these counts yourself
Copy this script and run it with Python 3.12 or later. It counts the same steps as the plot. Where a count depends on a random shuffle, yours will differ a little.
"""Count the looks of jump search yourself.
Run it with Python 3.12 or later. For each size n it searches the row
1, 2, ..., n and prints the looks for the number standing first, the
cheapest; for the last number of the last whole block, the dearest; and
the exact average when it is there, every place tried once, beside the
square root of n plus 1. The plot's average points are 20 places spread
over the row, within 2 per cent under that exact average. The counts are
the same on any computer; only the time differs.
"""
import math
def jump_search(row, target):
"""Find target in a row in order, smallest first, a block at a time.
Returns its place, counted from 0 as Python counts places, or None
when target is not in the row. The blocks hold the square root of
the row's length, rounded down, numbers each.
"""
size = max(1, math.isqrt(len(row))) # the block: the root, rounded down
for start in range(0, len(row), size): # each block in turn, from the left
end = min(start + size, len(row)) - 1 # the block's last place
if row[end] < target: # one look: is its last number smaller?
continue # then target is past this whole block
for place in range(start, end + 1): # look through this block
if row[place] == target: # one look: is this the number wanted?
return place # found, at the first place it stands
if row[place] > target: # the same look: is it bigger?
return None # then it is not in the row
return None # past the last block: it is not in the row
class Counted:
"""A number that counts the looks made at it.
A look asks one question first: is it smaller, at a block's last
number, or is it equal, in the walk. Is it bigger is asked in the
same look as the equal just before it, so it adds none.
"""
looks = 0
def __init__(self, value):
self.value = value
def __lt__(self, other):
Counted.looks += 1
return self.value < other.value
def __eq__(self, other):
Counted.looks += 1
return self.value == other.value
def __gt__(self, other):
return self.value > other.value
def looks(counted_row, target):
"""The looks for target in a row already made of Counted numbers."""
Counted.looks = 0
jump_search(counted_row, Counted(target))
return Counted.looks
def count(row, target):
"""How many looks jump search makes looking for target in row."""
return looks([Counted(x) for x in row], target)
if __name__ == "__main__":
print(f"{'n':>6} {'first':>6} {'dearest':>8}"
f" {'average':>8} {'root + 1':>9}")
for n in [10, 100, 1000, 10000]:
row = [Counted(x) for x in range(1, n + 1)] # any n numbers in order
size = math.isqrt(n)
dearest = size * (n // size) # the last of the last whole block
every = sum(looks(row, x) for x in range(1, n + 1)) / n
print(f"{n:>6} {looks(row, 1):>6} {looks(row, dearest):>8}"
f" {every:>8.2f} {math.sqrt(n) + 1:>9.2f}")
best, worst and average
Each case looks for a number in the row, no two equal. One search then costs from 2 looks to .
A case is one kind of input, the thing the method is given, such as a row back to front, whatever its length. A bound says how fast that case's cost grows as the input gets longer.
The counts are in the grid. A count is often the sum of pieces that grow at different speeds, and on long inputs the fastest-growing piece swamps the rest. For example, a count of half of n times n, less half of n: at 2,000 items, 2,000,000 less 1,000. So how a cost grows is what tells one method from another, and the bounds keep just that.
| bestthe number wanted standing first | averagea number in the row, no two equal, each place as likely | worstthe last number of the last whole block | |
|---|---|---|---|
| looks | 2, block 1's last number, then place 1 | about , exactly that on a square row, whose length is a number times itself: 4 on 9 numbers | , the last number of the last whole block: 6 on 10 numbers |
| how the looks grow in each case | |||
| tight, | |||
| ceiling, | |||
| floor, | |||
extra memory: four boxes beside the row, the block's size, its start and end, and the place reached, the same however long the row
Extra memory is the room it needs on top of the input itself, while it runs.
Looking for 5: 2 looks, found at place 1.
Looking for 70: 6 looks, found at place 9.
Looking for 84: 5 looks, found at place 10.
Looking for 66: 6 looks, not there.
Looking for 90: 4 looks, not there.
- a look, numbered in turn
- found, ringed
- looked at, not it
- ruled out without a look
- never reached
- a block passed whole
- the block looked through
- a block never reached
| numbers | block, | standing first | the most | average, every place | |
|---|---|---|---|---|---|
| 4 | 2 | 2 | 4 | 3 | 3 |
| 9 | 3 | 2 | 6 | 4 | 4 |
| 10 | 3 | 2 | 6 | 4.1 | 4.16 |
| 12 | 3 | 2 | 7 | 4.5 | 4.46 |
| 16 | 4 | 2 | 8 | 5 | 5 |
| 100 | 10 | 2 | 20 | 11 | 11 |
| 1,000 | 31 | 2 | 63 | 32.54 | 32.62 |
Looking for 26: 3 looks, found at place 3.
- a look, numbered in turn
- found, ringed
- looked at, not it
- ruled out without a look
- never reached
- a block passed whole
- the block looked through
- a block never reached
where the formula comes from
Each count above, worked out in small steps. Every step names the line it uses.
the move: watch the search
the line:Looking for 6: 2 looks, found at place 1.
- a look, numbered in turn
- found, ringed
- looked at, not it
- never reached
- the block looked through
- a block never reached
21, block 1's end, is not smaller than 6, so the walk starts at place 1 and finds it. 2 looks. why it is allowed: The first look is always at a block's end, and a number is found only in the walk. So no number of the row costs 1.
the move: count it
the line:why it is allowed: is the looks for the number standing first. By line 1, 2, for 9 numbers or 9 million.
guess it, thenthe move: a ceiling
the line:why it is allowed: By line 2. An equals gives a ceiling and a floor at once.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 3, 2 times 1 at most, whatever is.
guess it, thenthe move: a floor
the line:why it is allowed: By line 2 again.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 5.
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 4 and 6 bound by the same growth, 1, which is to say none.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: name the block
the line:why it is allowed: is the block's size, the square root of rounded down. The root of 10 is about 3.16, so 10 numbers make blocks of 3.
the move: price one place
the line:- looks for the number at each place, 10 numbers
Each place's cost on 10 numbers, 2, 3, 4, then 3, 4, 5, then 4, 5, 6, then 5. The top is place 9. why it is allowed: To reach block , the search looks at block ends, the last not smaller. The walk then looks at places. In section 3's picture, 70 is place 3 of block 3, so 6 looks, and 84 place 1 of block 4, so 5.
guess it, thenthe move: find the costliest
the line:place block, place in the block, looks, 7 3 1 3 + 1 = 4 8 3 2 3 + 2 = 5 9 3 3 3 + 3 = 6 10 4 1 4 + 1 = 5 The last four places of section 3's row of 10. why it is allowed: The whole blocks are over , rounded down: 3 for 10. In a whole block, is at most that and at most , both at its last place. A short last block has one more but one less at least, so it never costs more. On 10 numbers, the short block's one place costs 4 + 1, less than 3 + 3.
guess it, thenthe move: count it
the line:why it is allowed: is the most looks for a number in the row. By line 3, is the whole blocks and is . For 10, 3 + 3 = 6.
guess it, thenthe move: at most two blocks past m
the line:4 times 4 is 16, cut into 3 times 3, two strips of 3 and a corner of 1. Without the corner, 15 is 5 blocks of 3. the longest row with blocks of whole blocks 1 3 3 3 2 8 4 4 3 15 5 5 4 24 6 6 Just under each next square, the whole blocks reach , and no row has more. why it is allowed: Take of 3: rows of 9 to 15 numbers. The next square is 4 times 4, 16, and 15 is 5 blocks of 3. In general, the root of is under , as is the root rounded down, and rounding down takes off less than 1. So is under the next square, times . As the picture shows, that square is times , two strips of and a corner of 1. So is at most blocks of , and the most is the whole blocks, at most, plus the walk's .
guess it, thenthe move: m is at most the root
the line:why it is allowed: is rounded down, so at most . For 10, 2 times 3.16, plus 2, is about 8.3, over 6.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 6. From 1 number on, 2 is at most 2 times , so is at most 4 times .
guess it, thenthe move: at least m whole blocks
the line:why it is allowed: By lines 1 and 4. is at most the root of , so times is at most , at least whole blocks. For 10, 3 times 3 is 9, so 3 + 3.
guess it, thenthe move: m is at least 1
the line:why it is allowed: Every block holds 1 number at least.
guess it, thenthe move: rounded down, plus 1, passes the root
the line:why it is allowed: Rounding down takes off less than 1, so is more than . For 10, 4 is more than 3.16.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 10, 1 times at least, from 1 number on.
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 7 and 11 bound by the same growth, .
guess it, then
try it with a small : at , line 4 gives , which you can work out by hand
the move: price each place from below
the line:place place over 2 block place in the block looks its floor, place over 2, plus 1 1 0.5 1 1 2 1.5 2 1 1 2 3 2 3 1.5 2 1 3 2.5 4 2 2 2 4 3 in all 12 9 4 numbers in blocks of 2, the root of 4. Each block is at least the place over 2, and every place costs at least its floor. why it is allowed: By line 2 of the question on the most looks, place costs its block's number, , plus 1 at least. Blocks hold places at most, so is at least over , and is at most .
the move: average the floors
the line:why it is allowed: is the average looks for a number in the row, no two equal, each place as likely. By line 1 each place costs at least its floor, so the average does too. For 4, the floors 1.5, 2, 2.5 and 3 add to 9, and 9 over 4 is 2.25, under the 3 counted.
guess it, thenthe move: split the sum
the line:why it is allowed: Add the floors in two parts, each place's over the root, then a 1 for each of the places. For 4, 10 over the root of 4, 2, is 5, plus 4 is 9, and 9 over 4 is 2.25.
guess it, thenthe move: use the page on adding up 1 to n
the line:where does this come from?
question 1 of the page adding up Fit two staircases of squares together. What shape do they make? the move: draw the sum as a staircase
the line:why it is allowed: Let be the sum of the whole numbers from 1 to . Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to . For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.
the move: take two staircases
the line:why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.
the move: fit them into a rectangle
the line:Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15. why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds , and the columns fill a rectangle wide and tall.
the move: halve both sides
the line:why it is allowed: Halving both sides of line 3 keeps it true and leaves one . For 5, half of 30 is 15.
why it is allowed: The box under this line adds 1 to with two staircases, times , halved. For 4, 4 times 5 over 2 is 10, the same 10 as line 3.
guess it, thenthe move: divide by n
the line:why it is allowed: Divide each part by . For 4, 5 over twice 2 is 1.25, plus 1 is 2.25.
guess it, thenthe move: drop the 1s
the line:why it is allowed: Dropping the 1, and taking for , makes the right side smaller, so is still at least it. For 4, 4 over 4 is 1, under 2.25.
guess it, thenthe move: n over its root
the line:why it is allowed: over is , as times is . For 4, half of 2 is 1.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 7, half of at least, from 1 number on.
guess it, thenthe move: no search costs more than the most
the line:why it is allowed: By line 4 of the question on the most looks, no number in the row costs more, so neither does the average. For 4, 3 against 4.
guess it, thenthe move: use that question's ceiling
the line:why it is allowed: By lines 5 and 6 of the question on the most looks.
guess it, thenthe move: read it as a bound
the line:why it is allowed: By line 10, at most 4 times , from 1 number on.
guess it, thenthe move: ceiling and floor together
the line:why it is allowed: Lines 8 and 11 bound by the same growth, . The next question finds it exactly on a square row.
guess it, then
try it on small inputs: over the 4 places of a row of 4 different numbers, 12 looks in all, the average is
the move: price one place
the line:block place 1 place 2 place 3 in all 1 2 3 4 9 2 3 4 5 12 3 4 5 6 15 in all 36 Each cell is its block's number plus its place in the block. 36 looks over 9 places is 4. why it is allowed: By line 2 of the question on the most looks. On section 1's 9 numbers, in blocks of 3, every place's cost is in the table.
the move: average every place
the line:why it is allowed: is the average over the times places, each as likely. For 3, 36 over 9 is 4.
guess it, thenthe move: split the block numbers from the places
the line:block block numbers places in the block in all 1 1 + 1 + 1 = 3 1 + 2 + 3 = 6 9 2 2 + 2 + 2 = 6 1 + 2 + 3 = 6 12 3 3 + 3 + 3 = 9 1 + 2 + 3 = 6 15 in all 18 18 36 The table of costs split in two. Its block numbers add to 18, its places to 18, and 18 + 18 = 36. why it is allowed: Each block's number stands in cells of the table, and each place in the block in cells too, as the picture splits them. For 3, 3 times 6, plus 3 times 6, is 36, and 36 over 9 is 4.
guess it, thenthe move: share out 1 over m squared
the line:why it is allowed: over is 1 over . For 3, 6 over 3, twice.
guess it, thenthe move: use the page on expected cost
the line:where does this come from?
question 1 of the page expected costPick a whole number from 1 to , each as likely. What is its expected value? the move: each value times its chance
the line:times its chance 1 1 over 5 2 2 over 5 3 3 over 5 4 4 over 5 5 5 over 5 in all 15 over 5, which is 3 For 5, each number times its chance, 1 over 5. why it is allowed: The expected value adds each number times its chance, 1 over . , read the sum of, adds the term after it for each from the value under it to the value over it.
the move: take the chance out in front
the line:why it is allowed: Each term is multiplied by 1 over , so multiply once, after adding. For 5, 1 over 5 times 15.
the move: use the page on adding up
the line:where does this come from?
question 1 of the page adding up Fit two staircases of squares together. What shape do they make? the move: draw the sum as a staircase
the line:why it is allowed: Let be the sum of the whole numbers from 1 to . Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to . For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.
the move: take two staircases
the line:why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.
the move: fit them into a rectangle
the line:Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15. why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds , and the columns fill a rectangle wide and tall.
the move: halve both sides
the line:why it is allowed: Halving both sides of line 3 keeps it true and leaves one . For 5, half of 30 is 15.
why it is allowed: The whole numbers from 1 to add up to times one more than , halved, as the page on adding up 1 to works out. For 5, 5 times 6, halved, is 15.
the move: cancel the n
the line:why it is allowed: on top and underneath cancel, as is not zero. For 5, 6 over 2 is 3.
the move: read it
the line:why it is allowed: By line 4, halfway between the smallest number, 1, and the biggest, : 3 for 5.
why it is allowed: The box under this line works out the average of a number from 1 to its , each as likely, as one more than it, halved. Here, with in place of its , twice, for the block reached and for the place in it. For 3, 2 and 2.
guess it, thenthe move: add
the line:why it is allowed: Two halves of . For 3, 4.
guess it, thenthe move: m is the root of the row's length
the line:why it is allowed: The row holds , times , numbers, so is . For 9, 3 + 1 = 4, as section 3's table counts.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
the move: block ends, then the walk
the line:- block ends, 36 over
- the walk,
- the most, their sum
The ends fall as the blocks grow, and the walk climbs. Their sum is least at 6. block size b block ends walk the most average, every place 1 36 1 37 19.5 2 18 2 20 11 3 12 3 15 8.5 4 9 4 13 7.5 6 6 6 12 7 9 4 9 13 7.5 12 3 12 15 8.5 18 2 18 20 11 Blocks of 6, the root of 36, cost the fewest, at most and on average. Blocks of 36 cost 37, as blocks of 1 do. why it is allowed: is the most looks on 36 numbers in blocks of , for that divides 36. That is 36 over block ends, then in the walk. For 2, 18 + 2 = 20.
the move: write it round a square
the line:why it is allowed: To show never drops below 12, write it round a square. Multiply the square out. times is ; twice times 6 over is 12; 6 over , squared, is 36 over . So the square is , less 12, plus 36 over ; add 12. For 2, 2 less 12, plus 18, is 8, and 8 + 12 = 20.
guess it, thenthe move: a square is never below 0
the line:why it is allowed: By line 2, a square is 0 or more.
guess it, thenthe move: where the least is
the line:block size b blocks the most 3, the root rounded down 5 of 3 5 + 3 = 8 4 3 of 4, then 3 3 + 4 = 7 On 15 numbers, the root rounded down gives blocks of 3, yet blocks of 4 cost fewer looks. why it is allowed: The square is 0 when is 6 over , so when is 6, the root of 36. On 9 numbers it is the same: blocks of 3 cost 6, blocks of 1 or 9 cost 10.
guess it, thenthe move: this site's block
the line:why it is allowed: This site's code takes the root of , rounded down, as its block. That is its choice, not always the fewest looks.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
the move: what one look keeps
the line:The first look, at 30. 70 must be after it, so binary search keeps those 4 and drops the other 3. why it is allowed: Binary search looks at the middle, the first of two middles, and keeps the side the number must be on, the middle in neither side. Of these 8, the side after holds 4, the side before 3.
the move: count the halvings
the line:12 halved, rounding down, goes 6, 3 and 1. Three halvings, then one more look, so 4. why it is allowed: is binary search's most looks. By line 1 no look keeps more than half, and a smaller part never needs more, as the box under line 3 shows. A number above them all keeps the bigger half each time: the halvings to one number, and one more look.
guess it, thenthe move: use the page on halving
the line:where does this come from?
question 3 of the page halving and logarithmsBinary search on a row of numbers. At most how many looks? the move: what one look keeps
the line:8 numbers have two middles; the table's 7 have one. look still to search half, rounded down the middle kept 1 10, 20, 30, 40, 50, 60, 70 3.5 to 3 40 50, 60, 70 2 50, 60, 70 1.5 to 1 60 70 3 70 0.5 to 0 70 none 75 is above all 7 numbers, so each look keeps the side after. why it is allowed: This site's binary search looks at the middle of what is left, the first of two middles, and keeps the side the wanted number must be on. From 8 it keeps 3 before or 4 after: the side after holds half, rounded down, the side before no more.
the move: fewer numbers, no more looks
the line:after look keeping the bigger side keeping the smaller side 0 10 10 1 5 4 2 2 1 3 1 none 4 none Two searches on 10 numbers. Keeping the bigger side runs out after 4 looks, the smaller after 3. why it is allowed: Let A always keep the bigger side, and S be any search on the same row. While S holds no more than A, it keeps at most half of its part, rounded down, so no more than A keeps. In the table, S keeps 4 to A's 5, then 1 to 2. So S runs out no later.
the move: a number that keeps the bigger side
the line:why it is allowed: Each middle is too small, so each look keeps the side after it. That is A of line 2.
the move: count that search's looks
the line:why it is allowed: Each look halves, rounding down, so question 2 of the page halving and logarithms counts the looks to one number. 1 number takes no halving, log 0. For 7, 7 leaves 3, 3 leaves 1, and look 3 leaves none.
the move: the most looks
the line:why it is allowed: Let be the most looks, which lines 2 to 4 count. For 7 numbers, the search for 75 takes 3, as the table at line 1 shows.
the move: read it
the line:why it is allowed: For 7 numbers, 2 and 1 more. For 16, 4 and 1 more.
why it is allowed: The box under this line works out the most looks this way. For 12, 3 and 1 more is 4, its count in section 7's table.
guess it, then
try it with a small : at , line 2 gives , which you can work out by hand
count it yourself
Count every look for 70 in 5, 14, 22, 29, 37, 45, 53, 61, 70, 84.
| step | what happens | looks |
|---|---|---|
| 1 | There are 10 numbers in a row, in order, smallest first. Look for 70, jumping 3 numbers at a time. | 0 |
| 2 | Look at the last number of block 1, at place 3. It is 22. | 1 |
| 3 | 22 is smaller than 70, and nothing before it is bigger, so rule out the whole block, places 1 to 3. | 1 |
| 4 | Look at the last number of block 2, at place 6. It is 45. | 2 |
| 5 | 45 is smaller than 70, and nothing before it is bigger, so rule out the whole block, places 4 to 6. | 2 |
| 6 | Look at the last number of block 3, at place 9. It is 70. | 3 |
| 7 | 70 is not smaller than 70, so the first place 70 could stand is in this block. Look through it from place 7, as an earlier place in it may hold 70 too. | 3 |
| 8 | Look at place 7. It holds 53. | 4 |
| 9 | 53 is smaller than 70. Rule it out and move to the next. | 4 |
| 10 | Look at place 8. It holds 61. | 5 |
| 11 | 61 is smaller than 70. Rule it out and move to the next. | 5 |
| 12 | Look at place 9. It holds 70. | 6 |
| 13 | 70 is the number wanted, at place 9, so it is found. | 6 |
| 14 | Done after 6 looks. | 6 |
In all, 6 looks. The formula from section 4, , gives at : the same.
where you meet it
Jump search suits a row in order that is read from the front.
Records on tape
Shneiderman's 1978 paper finds this page's method the way to search records stored in blocks on tape, read from the front. There the middle record cannot be reached without reading every record before it. He calls it jump searching, noting it has also been called block search. He also names VSAM's file indexes as one place it is used.
source: B. Shneiderman, Jump Searching, a Fast Sequential Search Technique (opens in a new tab), Communications of the ACM 21(10), 1978, pages 831 to 834
Skip pointers in a search engine
A search engine keeps, for each word, the list of pages holding it, in order. Skip pointers let a search hop ahead along the list, each hop a jump to a block's end. The book below suggests about the square root of the list's length of them, evenly spaced. That is the balance of section 4's question on block sizes.
source: C. D. Manning, P. Raghavan and H. Schütze, Introduction to Information Retrieval, Cambridge University Press, 2008, section 2.3, Faster postings list intersection via skip pointers (opens in a new tab)
Blocks of keys on disk
LevelDB, a library that keeps keys in order on disk, splits them into blocks. Its index keeps one key a block, at least that block's last key, and LevelDB searches that index by binary search.
source: LevelDB (opens in a new tab)'s table format document, doc/table_format.md, and its code, table/block.cc and table/table_builder.cc
| entries in the list | skip pointers | one every |
|---|---|---|
| 100 | 10 | 10 entries |
| 10,000 | 100 | 100 entries |
| 1,000,000 | 1,000 | 1,000 entries |
Looking for 44: 5 looks, found at place 8.
- a look, numbered in turn
- found, ringed
- looked at, not it
- ruled out without a look
- never reached
- a block passed whole
- the block looked through
- a block never reached
what is used instead
Binary search, wherever any place of the row can be read at once. On ten million numbers it makes at most 24 looks, against jump search's 6,324. Shneiderman's paper too puts binary search first, and keeps jump searching for files it cannot search.
how it compares
Jump search beside three other searches. Each counts its looks.
| way | how it looks | needs the row in order | most looks on 12 numbers | average, numbers in the row, on 12 |
|---|---|---|---|---|
| linear search | one number at a time, from the left | no | 12 | 6.5 |
| binary search | the middle of what is left, keeping one side | yes | 4 | about 3.08 |
| jump search | a block at a time, then through the block | yes | 7 | 4.5 |
| interpolation search | guesses the place from the number's size | yes | 1 spread evenly, 10 bunched | 1 spread evenly, about 5.42 bunched |
- jump search at most,
- binary search at most,
- jump search at best, 2