Algorithms and Sequential Search
An algorithm is a finite set of instructions that accomplish a specific task. Every algorithm can be constructed using combinations of sequencing…
What an algorithm is
An algorithm is a finite set of instructions that accomplish a specific task.Every algorithm can be constructed using combinations of sequencing, selection, and iteration. You have been writing all three since the start of this course; this names them.
| Building block | What it means |
|---|---|
| Sequencing | the application of each step of an algorithm in the order in which the code statements are given (for example, following a baking recipe requires that steps are taken in a certain order) |
| Selection | determines which parts of an algorithm are executed based on a condition being true or false |
| Iteration | a repeating portion of an algorithm; it repeats a specified number of times or until a given condition is met |
Sequential search
Linear search, or sequential search, algorithms check each element of a list, in order, until the desired value is found or all elements in the list have been checked.Here it is implemented so that it returns the INDEX of the target, or -1 if it is not found.
Three things are worth pinning down about that middle call. The list holds a 3 at index 2 and another at index 5, and the answer is 2: the return fires on the FIRST match and the rest of the list is never looked at.