You've lost your friend's phone number, but you know it's somewhere in your contacts — a long, jumbled list that nobody ever sorted. How do you find it? You do the only thing that always works: you start at the top and read one name at a time — "not it… not it… not it…" — until either you spot the name you want, or you fall off the bottom of the list and admit it isn't there.
That plain, patient, check-everything method is a real algorithm with a real name: linear search. To find a value in a list, you look at each item in turn, in order, from the start:
It's called "linear" because you walk along the list in a straight line, one step after another. The huge advantage — the reason every programmer knows it — is that it makes no assumptions: the list can be sorted or a complete mess, numbers or names, and linear search still works. That is not true of the cleverer searches you'll meet later.
Here we're searching a list of numbers for the target 23. The highlighted box is the item we're checking right now; step through and watch the pointer march along until it lands on a match.
Every box before the match got a "no" — those are the comparisons we had to make. The item we
wanted sat at index
The algorithm is a single loop over the array (see
return its index —
which also stops the search early, because there's no point looking further. If the loop
finishes without ever returning, the value was never there, and by convention we return
-1 to mean "not found". Press Run and read the log of every
comparison it makes:
Notice the log stops the instant 8 (the very first item) and then to 99 (not
in the list at all), and re-run. You'll see the number of "Checking…" lines change dramatically.
Because linear search never relies on the order of the items, it happily searches a list of strings that are in no particular order. The code is the same shape; only the types change:
This list isn't alphabetical, and it doesn't matter one bit. That "works on any list" property is linear search's superpower — and, as we'll see, also its weakness.
The fair way to measure the work an algorithm does is to count its key operation. For a search,
that's the number of comparisons — how many items we check before we finish.
For a list of
The important takeaway: the worst-case work grows in direct proportion to the size of the
list. Double the list, and in the worst case you double the checks. Computer scientists write this
as
Every time the list doubles, so does the worst-case number of checks. That's what
When the item is in the list, you get to stop the moment you hit it. But to be certain
something is absent, you have no shortcut — you must inspect every last element
before you can honestly say "it's not here". So searching for a value that doesn't exist always
costs the full
Linear search is simple and universal, but it can be painfully slow on big lists.
Searching a phone book of
There's a catch, though: to go faster you usually need the list to be sorted first.
If your data is in order, a much cleverer method called