Brute Force

Using the example of Linear Search in an unsorted array, this lesson gives a gentle introduction to the brute force paradigm.

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 possible solutions 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 and no performance improvements at this stage.

Even though this approach of solving problems is the most inefficient one, it might be the first one that pops into 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 look at an example to 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.