What a brute force algorithm does
A brute force algorithm solves a problem by trying every possible candidate answer and checking each one against the goal. It doesn’t use shortcuts or rely on any clever insight into the problem’s structure. Because it checks everything, it finds a correct answer whenever one exists among the candidates. That is why it is often the first method people try, and why it serves as a baseline for judging faster methods.
The trade-off is speed. The number of candidates often grows very quickly as the input gets larger, so a method that is instant on a small input can become impractical on a large one.
Simple examples
Linear search
Checking each item in a list, one at a time, until you find the one you want is brute force. If the item is last in the list, or isn’t in the list at all, you have to check all n items.
Checking every pair
To find out whether any two numbers in a list are equal, you can compare every pair. A list of 10 numbers has 45 pairs, and a list of 20 has 190. The count grows with the square of the list length, so the work rises steeply as the list gets longer.
Trial division for primes
To test whether a whole number is prime, divide it by each possible factor from 2 up to its square root. If none of them divides it evenly, the number is prime. Trial division is simple and correct, and it works well for ordinary-sized numbers. For very large numbers, mathematicians and computer scientists use faster tests.
Closest pair of points
Given a set of points on a plane, the brute force way to find the two closest is to compute the distance between every pair and keep the smallest. The result is correct, but the number of distance calculations grows with the square of the number of points. Faster methods exist and are covered in most algorithms courses.
Ordering and the traveling salesman problem
Trying every possible order of a list to find a sorted one is brute force, and it is impractical. Ten items can be arranged in 3,628,800 different orders. Methods such as the heapsort algorithm sort a list of n items in roughly n log n steps instead.
The traveling salesman problem asks for the shortest route that visits every city once and returns to the start. A brute force solver tries every possible route. A route and its reverse have the same length, so 10 cities give 181,440 distinct round trips. The count grows very quickly from there. With 20 cities there are about 6 × 10^16 round trips, far more than can be checked in practice by brute force.
Password and PIN guessing
Brute force is also the basis of a common kind of attack. Someone trying to get into an account can work through every combination of a password or PIN until one works. A 4-digit PIN has 10,000 possibilities, from 0000 to 9999, so it can be guessed quickly if attempts are not limited. A longer, random password has far more possibilities. A 128-bit encryption key has 2^128 possibilities, which is far beyond what brute force can search in practice.
This is one reason login systems limit failed attempts, and why strong, unpredictable passwords matter.
Maximum subarray: brute force against a better method
Here is a problem that shows brute force and a faster method side by side. Given a list of numbers, find the continuous block of numbers with the largest sum.
The brute force approach checks every possible start and end position and adds up the numbers in between. The simplest version does about n³ steps. Keeping a running total cuts that to about n². Kadane’s algorithm, which is often described as a dynamic programming method, solves the same problem in a single pass through the list.
Why brute force slows down
Brute force time is roughly the number of candidates multiplied by the cost of checking each one. Doubling the input can multiply the work, not just double it. Pairs show this clearly: going from 10 items to 20 raises the pair count from 45 to 190. Orderings and routes grow even faster, which is why brute force is usually reserved for small inputs.
When brute force is still the right choice
Brute force is a sensible choice when:
- the input is small, so checking every candidate takes little time
- you need a reference answer to test a faster method against
- the task runs only once or rarely, and writing a faster method would take longer than the run
- correctness matters more than speed, and a simple method is easier to verify
Smarter methods
Faster approaches avoid checking every candidate. Greedy methods commit to the best-looking choice at each step. Dynamic programming reuses answers to smaller parts of a problem. Backtracking and branch and bound abandon paths that cannot lead to a good answer. Each one suits certain kinds of problems and has its own limits, so the choice depends on the problem at hand.