A brute-force algorithm solves a problem by trying every possible candidate, one by one, until it finds the right one. It uses no shortcuts and no clever mathematics. It is also called exhaustive search.
Example 1: opening a 4-digit lock
A 4-digit PIN has 10,000 possibilities. Brute force tries 0000, 0001, 0002, and so on up to 9999 until the lock opens:
secret = "4271"
for number in range(10000):
guess = f"{number:04d}" # 0000, 0001, ..., 9999
if guess == secret:
print("PIN:", guess, "| attempts:", number + 1)
breakPIN: 4271 | attempts: 4272Each extra digit makes the search ten times longer. This is why long passwords are much safer, and why many devices lock us out after a few wrong tries.
Example 2: two numbers with a given sum
Given the list [3, 8, 12, 5], which two numbers add up to 13? Brute force checks the pairs in order:
| Check | Pair | Sum | Equals 13? |
|---|---|---|---|
| 1 | 3 + 8 | 11 | no |
| 2 | 3 + 12 | 15 | no |
| 3 | 3 + 5 | 8 | no |
| 4 | 8 + 12 | 20 | no |
| 5 | 8 + 5 | 13 | yes |
In code, two nested loops do the work:
def two_sum(numbers, target):
checks = 0
for i in range(len(numbers)):
for j in range(i + 1, len(numbers)):
checks += 1
if numbers[i] + numbers[j] == target:
return numbers[i], numbers[j], checks
return None
print(two_sum([3, 8, 12, 5], 13))(8, 5, 5)The answer is 8 and 5, found on the 5th check.
How fast does the work grow?
A list of \(n\) numbers has this many pairs:
\[ \frac{n(n-1)}{2} \]
| Numbers | Pairs to check (at most) |
|---|---|
| 10 | 45 |
| 100 | 4,950 |
| 1,000 | 499,500 |
| 1,000,000 | 499,999,500,000 |
The time complexity is \(O(n^2)\): a list 10 times longer needs about 100 times more work in the worst case.
Example 3: finding similar documents
Suppose we have 1 million documents and want the pairs that are near-duplicates. Brute force compares document 1 with all the others, then document 2 with the rest, and so on.
That is about 500 billion comparisons, the last row of the table above. Even at a million comparisons per second, the job would take almost 6 days.
This bottleneck is why we use shingling, MinHashing and Locality-Sensitive Hashing (LSH). MinHashing makes each comparison cheaper, and LSH picks out the likely pairs so that we skip most of the others. In exchange, we accept that a few similar pairs may be missed.
Pros and cons
| Advantages | Disadvantages |
|---|---|
| Simple to write and understand, with little room for logic errors. | Slow on large inputs: the work may grow as \(n^2\), \(2^n\) or even \(n!\). |
| Exact: if an answer exists and we check every candidate, we find it. | Impractical at scale: fine for 100 items, perhaps hopeless for millions. |
| A good baseline for testing a faster, more complex algorithm on small inputs. | Wasteful: it checks even obviously bad candidates. |
When to use it
Brute force is a sensible choice when the input is small, when correctness matters more than speed, or when we need a trusted answer to test a better method against. When the input grows large, we look for a way to rule out candidates without checking each one.