Now in some cases, we’ll use a brute force method of searching. This is slow and inefficient from a run-time perspective. However, it is relatively fast to write.
With a brute force search, you scan through every item in a list until you find the element.
Why use Brute Force
As you can imagine, scanning through every item can be a time consuming process, especially if the list is large, so why would we do it?
Well, first, there is an issue with items not in an array (or Python List). If you are using a data structure like a linked list, then you know you cannot go to the middle element.
Second, as we work with complex data, who’s to say that is how we are sorted. Consider a class of employees. They are sorted by Employee ID, however, you are searching by their name. The ID and Name for the object may have nothing to do with one another. And even if they did, what’s the odds that other values like department, or years of service, match up with the Employee ID.
Finally, we may not write an efficient search algorithm if we rarely search – but that is pretty unlikely.
How to Create a Brute Force Search
The pseudo code for a brute search might look something like:
Function findEmployeeByName(search)
For employee in employeesList
If search = employee.lastName then
Return employee
End If
Loop
End Function
Of course, if we are using a simple array, then brute force is not likely a good choice. Rather, we’d want to sort the data, and then do a search if that was possible.
Brute Force Searching was originally found on Access 2 Learn