Theory

Watch: linear search, step by step

There is no code here. Before you write the search, watch what it does — on a library of five items where only the titles matter:

index      0       1        2        3       4
         [Aidas] [Bangos] [Ciklas] [Dūmai] [Ežeras]

You write nothing in this step. Read the tables top to bottom.

How we count

One comparison is one title check: "is this item the one I am looking for?" It is the same thing the code will do with c.Hit() before every check.

Searching for "Ciklas"

The algorithm stops at the first item that matches. Every row is one look:

looking for "Ciklas"
i=0   Aidas     ≠     comparisons: 1
i=1   Bangos    ≠     comparisons: 2
i=2   Ciklas    =     comparisons: 3    → found, return 2

Dūmai and Ežeras were never touched. Once it is found, nobody looks further.

Four cases side by side

looking for   where it is    comparisons
"Aidas"       index 0               1      best case
"Ciklas"      index 2               3
"Ežeras"      index 4               5      last item — worst case
"Nėra"        not there (-1)        5      a miss — also the worst case

Three observations. They are worth not skipping — the whole measurement in this lesson is just these three, multiplied by a bigger n.

1. On a hit, the comparison count is index + 1. Not "roughly" — exactly: index 0 costs 1, index 2 costs 3, index 4 costs 5.

2. A miss costs as much as the last item — all n. You cannot say "it is not there" without looking at everything: only the last comparison shows that the last item does not match either.

3. The cost depends on WHERE the item sits, not on what it is. Same algorithm, same slice — anything from 1 to 5. That is exactly the "best / worst case" step 1 talked about.

What comes next

Now you know what LinearFind has to do. In the next step you write it as Go code — and see the same numbers on a thousand items instead of five.