Introduction to the linear search algorithm
The linear search algorithm is a basic search within a data structure that begins by visiting the data at the beginning and visits each value until it’s found or reaches the end of the array, where the value does not exist. In the Introduction to the array data structure section, we did a linear search to find a picture in the example array. We’ll experiment with it more by using the same array of integers used in the selection sort algorithm. First, let’s write the algorithm in pseudocode:
- Store the value being searched for.
- Start at index 0.
- WHILE the value is not found and is not at end of the array.
- IF the value is equal to the value being searched for.
- Return the index
- ELSE
- Increase the index by 1 and move on to the next value.
C. IF at end of the array.
- Print value not found.
In the following example, we will be searching for the integer 48, so the algorithm will visit the value at index 0:
...