Skip to content

Jump search

finding

Level 2 of 3

Also called Block 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 nn long, rounded down, called mm: 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.

    6121322131284365446577658729block 1block 2block 3
  • Looking for 44: 5 looks, found at place 6.

    6113221312843365444625577658729block 1block 2block 3
  • Looking for 40: 5 looks, not there.

    6113221312843365444625577658729block 1block 2block 3
  • Looking for 80: 3 looks, not there.

    61132213128436544625776587293block 1block 2block 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
9 numbers in blocks of 3. 6 costs 2 looks: block 1's end, then place 1. 44 costs 5, looked at twice, as block 2's end and in the walk. 40 is not there: the walk stops at 44. 80 passes every block: 3 looks.
  • Looking for 51: 3 looks, found at place 2.

    241151223block 1block 2
  • a look, numbered in turn
  • found, ringed
  • looked at, not it
  • a block passed whole
  • the block looked through
Why 2 numbers can cost 3 looks. Each block is 1 number, so 51 is looked at as block 2's end, then again in the walk.
In a middle block: 8 moments of its 12 steps, the steps between them left for the watch page
  1. step 1 of 12

    71218233142495663

    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.

    watch from here

  2. step 2 of 12

    71218233142495663

    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

    watch from here

  3. step 3 of 12

    71218233142495663

    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

    watch from here

  4. step 4 of 12

    71218233142495663

    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

    watch from here

  5. step 5 of 12

    71218233142495663

    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

    watch from here

  6. step 6 of 12

    71218233142495663

    Look at place 4. It holds 23.

    what it remembers to know where it is
    looking for
    42
    block
    2
    looking at place
    4

    watch from here

  7. step 10 of 12

    71218233142495663

    Look at place 6. It holds 42.

    what it remembers to know where it is
    looking for
    42
    block
    2
    looking at place
    6

    watch from here

  8. step 11 of 12

    71218233142495663

    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

    watch from here

the steps, in plain words
  1. split the row into blocks of about √n numbersPython line 11
  2. look at the last number of the next blockPython lines 12, 13 and 14
  3. if it is smaller than the number wanted, rule out the whole blockPython lines 14 and 15
  4. otherwise look through this block from its startPython line 16
  5. if a number is the number wanted, it is foundPython lines 17 and 18
  6. if a number is bigger, the number wanted is not in the rowPython lines 19 and 20
  7. 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

slope 0.5102104101102103104105106107slope 0.5101102103104101102103104105106107slope 0.5101102103104101102103104105106107

nn, 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
what a dearest row costs, at a billion looks a second
nnlookstime
1,00063about 63 nanoseconds
100,000632about 632 nanoseconds
10,000,0006,324about 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 ⌊n/m⌋+m\lfloor n / m \rfloor + m.

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.

what each case costs, and how fast that cost grows; section 4 works out every formula in it
bestthe number wanted standing firstaveragea number in the row, no two equal, each place as likelyworstthe last number of the last whole block
looks2, block 1's last number, then place 1about n+1\sqrt{n} + 1, exactly that on a square row, whose length is a number times itself: 4 on 9 numbers⌊n/m⌋+m\lfloor n / m \rfloor + m, the last number of the last whole block: 6 on 10 numbers
how the looks grow in each case
tight, Θ\ThetaΘ(1)\Theta(1)Θ(n)\Theta(\sqrt{n})Θ(n)\Theta(\sqrt{n})
ceiling, OOO(1)O(1)O(n)O(\sqrt{n})O(n)O(\sqrt{n})
floor, Ω\OmegaΩ(1)\Omega(1)Ω(n)\Omega(\sqrt{n})Ω(n)\Omega(\sqrt{n})

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.

    51214222312943754565376187098410block 1block 2block 3block 4
  • Looking for 70: 6 looks, found at place 9.

    511422231294375456253746185709368410block 1block 2block 3block 4
  • Looking for 84: 5 looks, found at place 10.

    51142223129437545625376187093841045block 1block 2block 3block 4
  • Looking for 66: 6 looks, not there.

    511422231294375456253746185709368410block 1block 2block 3block 4
  • Looking for 90: 4 looks, not there.

    5114222312943754562537618709384104block 1block 2block 3block 4
  • 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
Standing first, 2 looks. 70, the last of the last whole block, costs the most: 3 block ends, then 3 places, 6. 84 stands last but costs 5: its block holds 1 number. Not in the row: 66 costs 6, 90 costs 4.
numbersblock, mmstanding firstthe mostaverage, every placen+1\sqrt{n} + 1
422433
932644
103264.14.16
123274.54.46
1642855
100102201111
1,0003126332.5432.62
Each average tries every place once, so it is exact. On a square row, 4, 9, 16 or 100 numbers, it is n+1\sqrt{n} + 1, as section 4's question on a square row shows.
  • Looking for 26: 3 looks, found at place 3.

    91172126332642415586637block 1block 2block 3block 4
  • 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
Equal numbers. Block 2's end holds the second 26, but the walk starts at place 3 and stops at the first.

where the formula comes from

Each count above, worked out in small steps. Every step names the line it uses.

Why does the number standing first cost 2 looks, however long the row?
  1. the move: watch the search

    the line:
    block 1’s last number,then place 1\begin{gathered} \text{block 1's last number,} \\ \text{then place 1} \end{gathered}
    • Looking for 6: 2 looks, found at place 1.

      6121322131284365446577658729block 1block 2block 3
    • 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.

  2. the move: count it

    the line:
    B=2B = 2

    why it is allowed: BB is the looks for the number standing first. By line 1, 2, for 9 numbers or 9 million.

  3. the move: a ceiling

    the line:
    B≤2B \le 2

    why it is allowed: By line 2. An equals gives a ceiling and a floor at once.

  4. the move: read it as a bound

    the line:
    B∈O(1)B \in O(1)

    why it is allowed: By line 3, 2 times 1 at most, whatever nn is.

  5. the move: a floor

    the line:
    B≥2B \ge 2

    why it is allowed: By line 2 again.

  6. the move: read it as a bound

    the line:
    B∈Ω(1)B \in \Omega(1)

    why it is allowed: By line 5.

  7. the move: ceiling and floor together

    the line:
    B∈Θ(1)B \in \Theta(1)

    why it is allowed: Lines 4 and 6 bound BB by the same growth, 1, which is to say none.

try it with a small nn: at n=9n = 9, line 2 gives B=2B = 2, which you can work out by hand

Which number costs the most looks, and how fast does that most grow?
  1. the move: name the block

    the line:
    m=⌊n⌋m = \lfloor \sqrt{n} \rfloor

    why it is allowed: mm is the block's size, the square root of nn rounded down. The root of 10 is about 3.16, so 10 numbers make blocks of 3.

  2. the move: price one place

    the line:
    place r of block jcosts j+r looks\begin{gathered} \text{place } r \text{ of block } j \\ \text{costs } j + r \text{ looks} \end{gathered}
    looksplace02460246810looksplace02460246810
    • 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 jj, the search looks at jj block ends, the last not smaller. The walk then looks at rr 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.

  3. the move: find the costliest

    the line:
    the costliest: the last placeof the last whole block\begin{gathered} \text{the costliest: the last place} \\ \text{of the last whole block} \end{gathered}
    placeblock, jjplace in the block, rrlooks, j+rj + r
    7313 + 1 = 4
    8323 + 2 = 5
    9333 + 3 = 6
    10414 + 1 = 5
    The last four places of section 3's row of 10.

    why it is allowed: The whole blocks are nn over mm, rounded down: 3 for 10. In a whole block, jj is at most that and rr at most mm, both at its last place. A short last block has jj one more but rr 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.

  4. the move: count it

    the line:
    W=⌊n/m⌋+mW = \lfloor n / m \rfloor + m

    why it is allowed: WW is the most looks for a number in the row. By line 3, jj is the whole blocks and rr is mm. For 10, 3 + 3 = 6.

  5. the move: at most two blocks past m

    the line:
    W≤2m+2W \le 2m + 2
    9331
    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.
    mmthe longest row with blocks of mmwhole blocksm+2m + 2
    1333
    2844
    31555
    42466
    Just under each next square, the whole blocks reach m+2m + 2, and no row has more.

    why it is allowed: Take mm 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 nn is under m+1m + 1, as mm is the root rounded down, and rounding down takes off less than 1. So nn is under the next square, m+1m + 1 times m+1m + 1. As the picture shows, that square is mm times mm, two strips of mm and a corner of 1. So nn is at most m+2m + 2 blocks of mm, and the most is the whole blocks, m+2m + 2 at most, plus the walk's mm.

  6. the move: m is at most the root

    the line:
    W≤2n+2W \le 2\sqrt{n} + 2

    why it is allowed: mm is n\sqrt{n} rounded down, so at most n\sqrt{n}. For 10, 2 times 3.16, plus 2, is about 8.3, over 6.

  7. the move: read it as a bound

    the line:
    W∈O(n)W \in O(\sqrt{n})

    why it is allowed: By line 6. From 1 number on, 2 is at most 2 times n\sqrt{n}, so WW is at most 4 times n\sqrt{n}.

  8. the move: at least m whole blocks

    the line:
    W≥m+mW \ge m + m

    why it is allowed: By lines 1 and 4. mm is at most the root of nn, so mm times mm is at most nn, at least mm whole blocks. For 10, 3 times 3 is 9, so 3 + 3.

  9. the move: m is at least 1

    the line:
    W≥m+1W \ge m + 1

    why it is allowed: Every block holds 1 number at least.

  10. the move: rounded down, plus 1, passes the root

    the line:
    W≥nW \ge \sqrt{n}

    why it is allowed: Rounding down takes off less than 1, so m+1m + 1 is more than n\sqrt{n}. For 10, 4 is more than 3.16.

  11. the move: read it as a bound

    the line:
    W∈Ω(n)W \in \Omega(\sqrt{n})

    why it is allowed: By line 10, 1 times n\sqrt{n} at least, from 1 number on.

  12. the move: ceiling and floor together

    the line:
    W∈Θ(n)W \in \Theta(\sqrt{n})

    why it is allowed: Lines 7 and 11 bound WW by the same growth, n\sqrt{n}.

try it with a small nn: at n=10n = 10, line 4 gives W=6W = 6, which you can work out by hand

Between which floor and ceiling does the average lie, when each place is as likely?
  1. the move: price each place from below

    the line:
    place p costs at leastp/n+1\begin{gathered} \text{place } p \text{ costs at least} \\ p / \sqrt{n} + 1 \end{gathered}
    placeplace over 2blockplace in the blocklooksits floor, place over 2, plus 1
    10.51121.5
    211232
    31.52132.5
    422243
    in all129
    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 pp costs its block's number, jj, plus 1 at least. Blocks hold mm places at most, so jj is at least pp over mm, and mm is at most n\sqrt{n}.

  2. the move: average the floors

    the line:
    A≥1n∑p=1n(pn+1)A \ge \frac{1}{n} \sum_{p=1}^{n} \left( \frac{p}{\sqrt{n}} + 1 \right)

    why it is allowed: AA 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.

  3. the move: split the sum

    the line:
    A≥1n(1n∑p=1np+n)A \ge \frac{1}{n} \left( \frac{1}{\sqrt{n}} \sum_{p=1}^{n} p + n \right)

    why it is allowed: Add the floors in two parts, each place's pp over the root, then a 1 for each of the nn places. For 4, 10 over the root of 4, 2, is 5, plus 4 is 9, and 9 over 4 is 2.25.

  4. the move: use the page on adding up 1 to n

    the line:
    A≥1n(n(n+1)2n+n)A \ge \frac{1}{n} \left( \frac{n(n + 1)}{2\sqrt{n}} + n \right)
    where does this come from?
    question 1 of the page adding up 1+2+⋯+n1 + 2 + \dots + nFit two staircases of squares together. What shape do they make?
    1. the move: draw the sum as a staircase

      the line:
      S=1+2+⋯+nS = 1 + 2 + \dots + n

      why it is allowed: Let SS be the sum of the whole numbers from 1 to nn. Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to nn. For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.

    2. the move: take two staircases

      the line:
      2S=2(1+2+⋯+n)2S = 2(1 + 2 + \dots + n)

      why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.

    3. the move: fit them into a rectangle

      the line:
      2S=n(n+1)2S = n(n + 1)
      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 nn 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 n+1n + 1, and the nn columns fill a rectangle nn wide and n+1n + 1 tall.

    4. the move: halve both sides

      the line:
      S=n(n+1)2S = \frac{n(n + 1)}{2}

      why it is allowed: Halving both sides of line 3 keeps it true and leaves one SS. For 5, half of 30 is 15.

    the whole page: adding up 1+2+⋯+n1 + 2 + \dots + n

    why it is allowed: The box under this line adds 1 to nn with two staircases, nn times n+1n + 1, halved. For 4, 4 times 5 over 2 is 10, the same 10 as line 3.

  5. the move: divide by n

    the line:
    A≥n+12n+1A \ge \frac{n + 1}{2\sqrt{n}} + 1

    why it is allowed: Divide each part by nn. For 4, 5 over twice 2 is 1.25, plus 1 is 2.25.

  6. the move: drop the 1s

    the line:
    A≥n2nA \ge \frac{n}{2\sqrt{n}}

    why it is allowed: Dropping the 1, and taking nn for n+1n + 1, makes the right side smaller, so AA is still at least it. For 4, 4 over 4 is 1, under 2.25.

  7. the move: n over its root

    the line:
    A≥n2A \ge \frac{\sqrt{n}}{2}

    why it is allowed: nn over n\sqrt{n} is n\sqrt{n}, as n\sqrt{n} times n\sqrt{n} is nn. For 4, half of 2 is 1.

  8. the move: read it as a bound

    the line:
    A∈Ω(n)A \in \Omega(\sqrt{n})

    why it is allowed: By line 7, half of n\sqrt{n} at least, from 1 number on.

  9. the move: no search costs more than the most

    the line:
    A≤⌊n/m⌋+mA \le \lfloor n / m \rfloor + m

    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.

  10. the move: use that question's ceiling

    the line:
    A≤2n+2A \le 2\sqrt{n} + 2

    why it is allowed: By lines 5 and 6 of the question on the most looks.

  11. the move: read it as a bound

    the line:
    A∈O(n)A \in O(\sqrt{n})

    why it is allowed: By line 10, at most 4 times n\sqrt{n}, from 1 number on.

  12. the move: ceiling and floor together

    the line:
    A∈Θ(n)A \in \Theta(\sqrt{n})

    why it is allowed: Lines 8 and 11 bound AA by the same growth, n\sqrt{n}. The next question finds it exactly on a square row.

try it on small inputs: over the 4 places of a row of 4 different numbers, 12 looks in all, the average is A=3A = 3

On a square row, mm times mm numbers, what is the average exactly?
  1. the move: price one place

    the line:
    place r of block jcosts j+r looks\begin{gathered} \text{place } r \text{ of block } j \\ \text{costs } j + r \text{ looks} \end{gathered}
    blockplace 1place 2place 3in all
    12349
    234512
    345615
    in all36
    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.

  2. the move: average every place

    the line:
    S=1m2∑j=1m∑r=1m(j+r)S = \frac{1}{m^2} \sum_{j=1}^{m} \sum_{r=1}^{m} (j + r)

    why it is allowed: SS is the average over the mm times mm places, each as likely. For 3, 36 over 9 is 4.

  3. the move: split the block numbers from the places

    the line:
    S=1m2(m∑j=1mj+m∑r=1mr)S = \frac{1}{m^2} \left( m \sum_{j=1}^{m} j + m \sum_{r=1}^{m} r \right)
    blockblock numbersplaces in the blockin all
    11 + 1 + 1 = 31 + 2 + 3 = 69
    22 + 2 + 2 = 61 + 2 + 3 = 612
    33 + 3 + 3 = 91 + 2 + 3 = 615
    in all181836
    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 mm cells of the table, and each place in the block in mm cells too, as the picture splits them. For 3, 3 times 6, plus 3 times 6, is 36, and 36 over 9 is 4.

  4. the move: share out 1 over m squared

    the line:
    S=1m∑j=1mj+1m∑r=1mrS = \frac{1}{m} \sum_{j=1}^{m} j + \frac{1}{m} \sum_{r=1}^{m} r

    why it is allowed: mm over m2m^2 is 1 over mm. For 3, 6 over 3, twice.

  5. the move: use the page on expected cost

    the line:
    S=m+12+m+12S = \frac{m + 1}{2} + \frac{m + 1}{2}
    where does this come from?
    question 1 of the page expected costPick a whole number from 1 to nn, each as likely. What is its expected value?
    1. the move: each value times its chance

      the line:
      E=∑k=1nk×1nE = \sum_{k=1}^{n} k \times \frac{1}{n}
      kktimes its chance
      11 over 5
      22 over 5
      33 over 5
      44 over 5
      55 over 5
      in all15 over 5, which is 3
      For 5, each number times its chance, 1 over 5.

      why it is allowed: The expected value EE adds each number kk times its chance, 1 over nn. Σ\Sigma, read the sum of, adds the term after it for each kk from the value under it to the value over it.

    2. the move: take the chance out in front

      the line:
      E=1n∑k=1nkE = \frac{1}{n} \sum_{k=1}^{n} k

      why it is allowed: Each term is multiplied by 1 over nn, so multiply once, after adding. For 5, 1 over 5 times 15.

    3. the move: use the page on adding up

      the line:
      E=1n×n(n+1)2E = \frac{1}{n} \times \frac{n(n + 1)}{2}
      where does this come from?
      question 1 of the page adding up 1+2+⋯+n1 + 2 + \dots + nFit two staircases of squares together. What shape do they make?
      1. the move: draw the sum as a staircase

        the line:
        S=1+2+⋯+nS = 1 + 2 + \dots + n

        why it is allowed: Let SS be the sum of the whole numbers from 1 to nn. Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to nn. For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.

      2. the move: take two staircases

        the line:
        2S=2(1+2+⋯+n)2S = 2(1 + 2 + \dots + n)

        why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.

      3. the move: fit them into a rectangle

        the line:
        2S=n(n+1)2S = n(n + 1)
        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 nn 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 n+1n + 1, and the nn columns fill a rectangle nn wide and n+1n + 1 tall.

      4. the move: halve both sides

        the line:
        S=n(n+1)2S = \frac{n(n + 1)}{2}

        why it is allowed: Halving both sides of line 3 keeps it true and leaves one SS. For 5, half of 30 is 15.

      the whole page: adding up 1+2+⋯+n1 + 2 + \dots + n

      why it is allowed: The whole numbers from 1 to nn add up to nn times one more than nn, halved, as the page on adding up 1 to nn works out. For 5, 5 times 6, halved, is 15.

    4. the move: cancel the n

      the line:
      E=n+12E = \frac{n + 1}{2}

      why it is allowed: nn on top and nn underneath cancel, as nn is not zero. For 5, 6 over 2 is 3.

    5. the move: read it

      the line:
      the middle of1 and n\begin{gathered} \text{the middle of} \\ \text{1 and } n \end{gathered}

      why it is allowed: By line 4, halfway between the smallest number, 1, and the biggest, nn: 3 for 5.

    the whole page: expected cost

    why it is allowed: The box under this line works out the average of a number from 1 to its nn, each as likely, as one more than it, halved. Here, with mm in place of its nn, twice, for the block reached and for the place in it. For 3, 2 and 2.

  6. the move: add

    the line:
    S=m+1S = m + 1

    why it is allowed: Two halves of m+1m + 1. For 3, 4.

  7. the move: m is the root of the row's length

    the line:
    S=n+1S = \sqrt{n} + 1

    why it is allowed: The row holds nn, mm times mm, numbers, so mm is n\sqrt{n}. For 9, 3 + 1 = 4, as section 3's table counts.

try it with a small mm: at m=3m = 3, line 2 gives S=4S = 4, which you can work out by hand

On 36 numbers, of the block sizes that divide 36, which makes the most looks fewest?
  1. the move: block ends, then the walk

    the line:
    T=36b+bT = \frac{36}{b} + b
    looksblock size b010203040010203040looksblock size b010203040010203040
    • block ends, 36 over bb
    • the walk, bb
    • the most, their sum
    The ends fall as the blocks grow, and the walk climbs. Their sum is least at 6.
    block size bblock endswalkthe mostaverage, every place
    13613719.5
    21822011
    3123158.5
    494137.5
    666127
    949137.5
    12312158.5
    182182011
    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: TT is the most looks on 36 numbers in blocks of bb, for bb that divides 36. That is 36 over bb block ends, then bb in the walk. For 2, 18 + 2 = 20.

  2. the move: write it round a square

    the line:
    T=(b−6b)2+12T = \left( \sqrt{b} - \frac{6}{\sqrt{b}} \right)^2 + 12

    why it is allowed: To show TT never drops below 12, write it round a square. Multiply the square out. b\sqrt{b} times b\sqrt{b} is bb; twice b\sqrt{b} times 6 over b\sqrt{b} is 12; 6 over b\sqrt{b}, squared, is 36 over bb. So the square is bb, less 12, plus 36 over bb; add 12. For 2, 2 less 12, plus 18, is 8, and 8 + 12 = 20.

  3. the move: a square is never below 0

    the line:
    T≥12T \ge 12

    why it is allowed: By line 2, a square is 0 or more.

  4. the move: where the least is

    the line:
    least when the square is 0,at b of 6\begin{gathered} \text{least when the square is 0,} \\ \text{at } b \text{ of } 6 \end{gathered}
    block size bblocksthe most
    3, the root rounded down5 of 35 + 3 = 8
    43 of 4, then 33 + 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 b\sqrt{b} is 6 over b\sqrt{b}, so when bb 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.

  5. the move: this site's block

    the line:
    this site roundsthe root down\begin{gathered} \text{this site rounds} \\ \text{the root down} \end{gathered}

    why it is allowed: This site's code takes the root of nn, rounded down, as its block. That is its choice, not always the fewest looks.

try it with a small bb: at b=6b = 6, line 1 gives T=12T = 12, which you can work out by hand

At most how many looks does binary search make on a row of nn numbers in order? Section 7 compares it with jump search.
  1. the move: what one look keeps

    the line:
    each look keeps at mosthalf, rounded down\begin{gathered} \text{each look keeps at most} \\ \text{half, rounded down} \end{gathered}
    two middleslooking for 705132230414958663 beforedropped4 afterkeptthe look, in neither sidethe other middle
    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.

  2. the move: count the halvings

    the line:
    H=halvings to one number+1 look at it\begin{gathered} H = \text{halvings to one number} \\ + 1 \text{ look at it} \end{gathered}
    rounding down12631.5 → 13 halvings
    12 halved, rounding down, goes 6, 3 and 1. Three halvings, then one more look, so 4.

    why it is allowed: HH 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.

  3. the move: use the page on halving

    the line:
    H=⌊log⁡2 n⌋+1H = \lfloor \log_2\, n \rfloor + 1
    where does this come from?
    question 3 of the page halving and logarithmsBinary search on a row of nn numbers. At most how many looks?
    1. the move: what one look keeps

      the line:
      at most half, rounded down,of what is left\begin{gathered} \text{at most half, rounded down,} \\ \text{of what is left} \end{gathered}
      two middleslooking for 7510203040506070803 beforedropped4 afterkeptthe look, in neither sidethe other middle
      8 numbers have two middles; the table's 7 have one.
      lookstill to searchhalf, rounded downthe middlekept
      110, 20, 30, 40, 50, 60, 703.5 to 34050, 60, 70
      250, 60, 701.5 to 16070
      3700.5 to 070none
      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.

    2. the move: fewer numbers, no more looks

      the line:
      keeping a smaller part nevercosts more looks\begin{gathered} \text{keeping a smaller part never} \\ \text{costs more looks} \end{gathered}
      after lookkeeping the bigger sidekeeping the smaller side
      01010
      154
      221
      31none
      4none
      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.

    3. the move: a number that keeps the bigger side

      the line:
      a number above them allkeeps the bigger side\begin{gathered} \text{a number above them all} \\ \text{keeps the bigger side} \end{gathered}

      why it is allowed: Each middle is too small, so each look keeps the side after it. That is A of line 2.

    4. the move: count that search's looks

      the line:
      ⌊log⁡2 n⌋ looks leaveone number, one morelook leaves none\begin{gathered} \lfloor \log_2\, n \rfloor \text{ looks leave} \\ \text{one number, one more} \\ \text{look leaves none} \end{gathered}

      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.

    5. the move: the most looks

      the line:
      B=⌊log⁡2 n⌋+1B = \lfloor \log_2\, n \rfloor + 1

      why it is allowed: Let BB 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.

    6. the move: read it

      the line:
      one more look thanthe log rounded down\begin{gathered} \text{one more look than} \\ \text{the log rounded down} \end{gathered}

      why it is allowed: For 7 numbers, 2 and 1 more. For 16, 4 and 1 more.

    the whole page: halving and logarithms

    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.

try it with a small nn: at n=12n = 12, line 2 gives H=4H = 4, 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.

5, 14, 22, 29, 37, 45, 53, 61, 70, 84; 70, every step written down
stepwhat happenslooks
1There are 10 numbers in a row, in order, smallest first. Look for 70, jumping 3 numbers at a time.0
2Look at the last number of block 1, at place 3. It is 22.1
322 is smaller than 70, and nothing before it is bigger, so rule out the whole block, places 1 to 3.1
4Look at the last number of block 2, at place 6. It is 45.2
545 is smaller than 70, and nothing before it is bigger, so rule out the whole block, places 4 to 6.2
6Look at the last number of block 3, at place 9. It is 70.3
770 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
8Look at place 7. It holds 53.4
953 is smaller than 70. Rule it out and move to the next.4
10Look at place 8. It holds 61.5
1161 is smaller than 70. Rule it out and move to the next.5
12Look at place 9. It holds 70.6
1370 is the number wanted, at place 9, so it is found.6
14Done after 6 looks.6

In all, 6 looks. The formula from section 4, ⌊n/m⌋+m\lfloor n / m \rfloor + m, gives W=6W = 6 at n=10n = 10: 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 listskip pointersone every
1001010 entries
10,000100100 entries
1,000,0001,0001,000 entries
The book's square root rule, worked for three lists.
  • Looking for 44: 5 looks, found at place 8.

    419215312142653362387444855193571062117012block 1block 2block 3block 4
  • 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
12 page numbers, a skip pointer every 3 entries, the root of 12 rounded down. The block ends, 15, 33, 51 and 70, play the index, one key a block. Each hop is a jump: page 44 costs 3 block ends, then 38 and 44, 5 looks.

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.

wayhow it looksneeds the row in ordermost looks on 12 numbersaverage, numbers in the row, on 12
linear searchone number at a time, from the leftno126.5
binary searchthe middle of what is left, keeping one sideyes4about 3.08
jump searcha block at a time, then through the blockyes74.5
interpolation searchguesses the place from the number's sizeyes1 spread evenly, 10 bunched1 spread evenly, about 5.42 bunched
Each count is that search's own run on 12 numbers in order. The most is over searches for every number from 1 to 99, there or not. The average is over the 12 that are there. One row is spread evenly, 8, 16, 24 and on to 96. One is bunched, 1 to 11 then 99.
102104101102103104105106107101102103104101102103104105106107
  • jump search at most, ⌊n/m⌋+m\lfloor n / m \rfloor + m
  • binary search at most, ⌊log⁡2 n⌋+1\lfloor \log_2\, n \rfloor + 1
  • jump search at best, 2
A hundred times the numbers, ten times jump search's most; binary search's barely rises. At ten million numbers, 6,324 looks against 24.

race them on one row of your own