Linear search can be used to search for a desired value in a list. It achieves this by examining each of the elements and comparing it with the search element starting with the first element to the last element in the list until it finds a match.
The steps are:
- Examine the first element of the list.
- If the first element is equal to the target value, stop.
- If the first element is not equal to the target value, check the next element in the list.
- Continue steps 1-3 until the element is found or the end of the list is reached.
For example, we can use the linear search algorithm to find the target value 22 in a list. The algorithm iteratively moves through the list until it finds a 22 in the 7th position of the list.
Use linear search to find 23 in the list of numbers on the right. It appears twice. Would linear search stop when it reaches the third element or ninth element of the list?