Member-only story
Linear Search
If this isn’t the one you are looking for, try the next
This is a rather basic searching algorithm, but it is actually useful in practice when you just need to find an element in an array — and you suspect the element is towards the beginning of the array (or just don’t want to think too hard)
Difficulty: Beginner | Easy | Normal | Challenging
Prerequisites:
- Know what an algorithm is (unsure? Guide HERE)
- Be able to read Pseudocode
- Know what an array is (Guide HERE)
Terminology
Iteration: Each time a set of instructions is executed in a loop is an iteration
Linear Search: An algorithm to look for a target within an array, starting with the first item and moving through the next one in turn until the element is found
Why do we need to search
To find an item on a computer, we need to think like a computer. Each time a computer checks to see if the item is the one it wants, that is a comparison.
And here is the thing: Comparisons cost time.
Now computers are inherently ordered things. This might be surprising, but essentially what I’m saying is that…