Brute Force
This lesson gives a gentle introduction to the Brute Force paradigm using Linear Search in an unsorted array.
We'll cover the following
Brute Force Method
Let’s start off our discussion on algorithms with the most straightforward and exhaustive option—the Brute Force approach to solving a problem. This method requires us to go through all the possibilities to find a solution to the problem we are meaning to solve. For instance, if we have an array of integers and we want to find the minimum, maximum, or a certain element in that array, the Brute Force approach requires us to go through all the elements to find that specific element. There are no shortcuts, no performance improvements; not at this stage.
Even though this approach of solving problems is the most inefficient one, it might be the first one that pops up in our heads. Also, the good thing about the Brute Force method is that if a solution to our problem exists, we are guaranteed to find it this way. Once we have a way to go about solving the problem, we can focus on making the solution more efficient.
With this in mind, let’s take an example that might help you visualize this technique.
Linear Search
Linear/Sequential Search is a method for finding a target value within a given array. It sequentially checks each element of the array for the target value until a match is found or all the elements have been searched. Linear Search runs in linear time (at its worst) and makes n
comparisons (at most), where n
is the length of the list.
Let’s assume that we are given the following list (array) of unsorted integers.
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.