June 2020
Intermediate to advanced
382 pages
11h 39m
English
One of the simplest strategies for searching data is to simply loop through each element looking for the target. Each data point is searched for a match and when a match is found, the results are returned and the algorithm exits the loop. Otherwise, the algorithm keeps on searching until it reaches the end of the data. The obvious disadvantage of linear search is that it is very slow due to the inherent exhaustive search. The advantage is that the data does not need to be sorted, as required by the other algorithms presented in this chapter.
Let's look at the code for linear search:
def LinearSearch(list, item): index = 0 found = False # Match the value with each data element while index < len(list) and found is False: