Skip to content

Linear search

finding

Level 1 of 3

Also called Sequential search.

test yourself

Six questions, four to pick from and two for paper. Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

Linear search looks for 47 in 18, 47, 90, 47, 6. At which place does it stop?

184790476
the answer, and why

the answer is C

It looks at 18, then 47 at place 2, which is the number wanted. It stops there.

  • Looking for 47: 2 looks, found at place 2.

    1811472290347465
  • a look, numbered in turn
  • found, ringed
  • looked at, not it
  • never reached
Two looks. The 47 at place 4 is never reached.

if you chose another

  • That is the second 47. The search stops at the first.
  • Place 1 holds 18, which is not 47.
  • Place 5 is reached only when the number is not there, or stands last. Here it stands at place 2.

question 2

How many looks does linear search take to learn that 52 is not in 31, 8, 77, 64, 19, 45, 3?

318776419453
the answer, and why

the answer is B

52 is not there, so every one of the 7 numbers is looked at, once each.

  • Looking for 52: 7 looks, not there.

    31118227733644419554566377
  • a look, numbered in turn
  • looked at, not it
Seven looks, one at each place, and none of them finds 52.

if you chose another

  • One look finds a number only when it stands first.
  • That is the average for a number that is there, (7 + 1) over 2.
  • There is no place 8. The search stops after place 7.

question 3

Which search costs linear search the fewest looks? Each option is a row, then, after the semicolon, the number wanted.

the answer, and why

the answer is D

40 stands at place 3, so 3 looks find it. The other three searches each look at all 5 numbers.

  • Looking for 67: 5 looks, found at place 5.

    26118122403313446755
  • Looking for 40: 3 looks, found at place 3.

    261181224033134675
  • Looking for 55: 5 looks, not there.

    26118122403313446755
  • Looking for 26: 5 looks, found at place 5.

    67118122403313442655
  • a look, numbered in turn
  • found, ringed
  • looked at, not it
  • never reached
The four options in order, a to d. Only b stops before the last place, at its third look.

if you chose another

  • 67 stands last, 5 looks.
  • 55 is not there, 5 looks.
  • 26 stands last in this order, 5 looks.

question 4

Which is true of linear search's looks?

the answer, and why

the answer is C

Standing first costs 1 look, Θ(1)\Theta(1). Not there costs nn, Θ(n)\Theta(n).

this tests best, worst and average, on the learn page

if you chose another

  • That gives the best case the worst case's cost. The best case is the row where the number stands first.
  • The search never uses the order, so every number is still looked at.
  • On average it is n+12\frac{n + 1}{2}, about half the row.

pen and paper

question 5about 8 minutes

Linear search looks for a number that is in the row 34, 81, 12, 57, 6, 95, each place as likely.

  1. Count the looks for the number at each place.
  2. Average them.
  3. Check your average against n+12\frac{n + 1}{2}.
a hint

The number at place kk costs kk looks.

a worked answer

The six places cost 1, 2, 3, 4, 5 and 6 looks: 21 in all. Over 6 places that is 3.5. The formula gives 7 over 2, also 3.5.

placenumberlooks
1341
2812
3123
4574
565
6956
in all21
on average21 over 6, which is 3.5
Each number of 34, 81, 12, 57, 6, 95 looked for in turn.
Why does a number that is there cost n+12\frac{n + 1}{2} looks on average, each place as likely?
  1. the move: price one place

    the line:
    the number at place kcosts k looks\begin{gathered} \text{the number at place } k \\ \text{costs } k \text{ looks} \end{gathered}
    • Looking for 9: 1 look, found at place 1.

      911412263704135
    • Looking for 41: 2 looks, found at place 2.

      9114122263704135
    • Looking for 26: 3 looks, found at place 3.

      91141222633704135
    • Looking for 70: 4 looks, found at place 4.

      911412226337044135
    • Looking for 13: 5 looks, found at place 5.

      9114122263370441355
    • a look, numbered in turn
    • found, ringed
    • looked at, not it
    • never reached
    Each of the 5 numbers looked for in turn. The number at place 3 costs 3 looks.

    why it is allowed: Here kk is the place the number stands at, once in the row. The search looks at places 1 to kk, then stops. With repeats it stops sooner, as section 3 shows.

  2. the move: average over the places

    the line:
    A=(1+2+⋯+n)×1nA = (1 + 2 + \dots + n) \times \frac{1}{n}
    placelookschancelooks times its chance
    11a fifth1 fifth
    22a fifth2 fifths
    33a fifth3 fifths
    44a fifth4 fifths
    55a fifth5 fifths
    in all155 fifths, which is 115 fifths, which is 3
    The 5 numbers of line 1, each as likely. The chances add up to 1, and the last column to 3, the average.

    why it is allowed: AA is the average looks. By line 1 the places cost 1, 2 and on up to nn looks. Each place has chance 1 over nn, so their total is shared by nn.

  3. the move: add 1 to n

    the line:
    A=n(n+1)2×1nA = \frac{n(n + 1)}{2} \times \frac{1}{n}
    The looks for places 1 to 5 stand as columns of 1 to 5. The same, hung upside down, fill a rectangle 5 wide and 6 tall, 30 squares. Half is 15, the looks in all.
    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, as the picture does for 5. For 5, 5 times 6 over 2 is 15.

  4. the move: share by n

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

    why it is allowed: Sharing by nn takes away the nn that multiplies n+1n + 1. For 5, 15 over 5 is 3, the table's average.

  5. the move: a floor

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

    why it is allowed: In line 4, n+1n + 1 is more than nn.

  6. the move: read it as a bound

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

    why it is allowed: By line 5, at least half of nn.

  7. the move: a ceiling

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

    why it is allowed: In line 4, 1 is at most nn.

  8. the move: halve 2n

    the line:
    A≤nA \le n

    why it is allowed: n+nn + n over 2 is nn.

  9. the move: read it as a bound

    the line:
    A∈O(n)A \in O(n)

    why it is allowed: By line 8.

  10. the move: ceiling and floor together

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

    why it is allowed: Lines 6 and 9 bound AA by the same growth, nn. On average, about half the row.

try it on small inputs: over the number at each of 5 places, 15 looks in all, the average is A=3A = 3

this tests where the formula comes from, on the learn page

what a full answer includes
  • Each place costing its own number of looks.
  • The total 21 and the average 3.5.
  • The formula giving 3.5 too.

question 6about 8 minutes

A shop's till keeps its 40 price codes in a list in no order, each code once. It searches the list with linear search for 300 sales a day, each code as likely. 20 times a day a mistyped code, not on the list, is searched for too.

  1. How many looks does one sale take on average?
  2. About how many looks do the 300 sales take in a day?
  3. How many more do the 20 mistyped codes take?
a hint

A code on the list costs n+12\frac{n + 1}{2} looks on average; one not there costs nn.

a worked answer

One sale averages 41 over 2, which is 20.5 looks. 300 sales take about 300 times that, 6,150 looks. A mistyped code is not there, so it costs all 40 looks: 20 of them take 800 more.

searcheslooks eachlooks in all
300 sales, on the list20.5 on averageabout 6,150
20 mistyped, not there40800
the dayabout 6,950
A day at the till, 40 codes in no order.

this tests where the formula comes from, on the learn page

what a full answer includes
  • 20.5 from 41 over 2.
  • About 6,150 for the sales.
  • 800 for the mistyped codes, 40 each.