Explain the Brute Force Method of Algorithm Design with Suitable Examples

Explain the Brute Force Method of Algorithm Design with Suitable Examples

Verified Sources
Oct 10, 2026

Brute-force search (also called exhaustive search) is a general algorithm design paradigm that solves a problem by enumerating all candidates and verifying which one(s) satisfy the goal condition. It is “generate and test” in spirit: systematically generate possibilities, test each, and return the first valid (satisficing) or the best among them (optimization).

Because it tries everything, brute force is typically easy to implement and always correct, but it often becomes impractical due to rapid growth in the number of candidates (e.g., exponential or factorial). For instance, brute-force substring matching has a worst-case time of O(mn)O(mn) for a pattern of length mm in a text of length nn. Similarly, the eight queens brute-force idea is to examine all arrangements and check validity.

Key term set: candidate space, verification predicate, generate-and-test, worst-case complexity.

Footnotes

  1. Brute-force search - Wikipedia - Defines brute-force/exhaustive search as systematically checking all possible candidates; includes examples like divisors and eight queens and discusses candidate explosion. ↩ ↩2

  2. Medium/Brute Force and Exhaustive Search - Explains brute-force string matching and states the worst-case is O(nm)O(nm) with pseudocode. ↩

Core idea and “algorithm skeleton”

A brute-force algorithm usually follows this structure:

  1. Identify what a candidate is (a permutation, subset, route, substring alignment, etc.).
  2. Enumerate all candidates in the candidate space.
  3. For each candidate, use a verification predicate to decide if it meets the requirement.
  4. Return the correct answer (first valid) or the optimum (best valid).

This “blind iteration” characterization is a standard view of brute force as a technique that “blindly iterates an entire domain of possible solutions” without shortcuts to improve performance.

Key term set: verification predicate, candidate generation, satisficing, optimization.

Footnotes

  1. ALGORITHMS DESIGN STRATEGIES (Chapter 5 PDF) - Provides a classification of algorithm design strategies; describes brute force as iterating through a domain of solutions without shortcuts. ↩

Designing a Brute Force Solution (Generate → Test → Return)

  1. 1
    Step 1

    Decide the unit you will enumerate (e.g., an entire path, a subset, a permutation, a substring alignment).

  2. 2
    Step 2

    Determine how many candidates exist (e.g., 2n2^n subsets, n!n! permutations, (n−m+1)(n-m+1) substring alignments). Candidate counts drive runtime growth.

    Footnotes

    1. Brute-force search - Wikipedia - Defines brute-force/exhaustive search as systematically checking all possible candidates; includes examples like divisors and eight queens and discusses candidate explosion. ↩

  3. 3
    Step 3

    Implement a function that returns true/false (or a score) for a candidate. Example: check if two queens attack each other; check if sums equal target.

  4. 4
    Step 4

    Use nested loops/recursive generation to traverse the full candidate space without skipping possibilities.

  5. 5
    Step 5

    If the problem asks for any valid solution, stop at the first that passes. If it asks for the best, keep the best score seen so far.

Example 1: Linear search (brute force on an array)

Although linear search is often taught separately, it is still brute-force search over the candidate set “each index.” You check elements one-by-one until you find the key. A brute-force/linear approach has worst-case Θ(n)\Theta(n) comparisons because you may scan the entire list when the key is absent.

Key term set: linear scan, sequential search, key (search target).

Footnotes

  1. DEV Community: Brute Force Algorithms: The Power of Exhaustive Search - Includes linear/sequential search as a brute-force example and notes worst-case Θ(nn). ↩

Example 2: Brute-force substring pattern matching (O(mn)O(mn) worst case)

Given text TT of length nn and pattern PP of length mm, the brute-force method aligns PP at every position ii from 00 to n−mn-m, compares characters, and succeeds if all mm characters match. In the worst case, each alignment compares up to mm characters, for about (n−m+1)⋅m=O(mn)(n-m+1)\cdot m = O(mn) comparisons.

Key term set: substring alignment, character comparisons, worst-case bound.

Footnotes

  1. Medium/Brute Force and Exhaustive Search - Explains brute-force string matching and states the worst-case is O(nm)O(nm) with pseudocode. ↩

Example 3: Subset Sum via exhaustive enumeration (2n2^n subsets)

For a set of nn numbers and a target TT, brute force tests every subset and checks whether the subset sum equals TT. Because a set of nn elements has 2n2^n subsets, this runs in exponential time.

A worked micro-example conceptually:

  • Set: {3,1,7,9}\{3,1,7,9\}, Target: 1010
  • Check subset sums until you find a subset such as {1,9}\{1,9\} that totals 1010 (among other subsets).

Key term set: [power set]{def="Set of all subsets of a given set; size is 2n2^n."}, subset enumeration, [exponential time]{def="Runtime that grows as cnc^n for some constant c."}.

Footnotes

  1. Subset Sum Problem — Definition, Formula & Examples (Mathwords) - States brute-force checks all subsets; since there are 2n2^n subsets, it runs in exponential time. ↩

  2. Subset Sum Problem (SlideShare example) - Provides an example of subset sum brute force (e.g., {1,9} sums to 10) and describes trying all subsets. ↩

Example 4: Traveling Salesman Problem (TSP) with permutations

For TSP, brute force enumerates all possible routes (permutations of cities), computes total route length, and returns the minimum. This approach is correct but becomes infeasible as nn grows because the number of permutations grows very fast (factorially). Brute force for TSP is commonly described as “calculate the total distance for every possible route and then select the shortest one,” but it is not efficient for large instances.

Key term set: route permutation, total distance objective, [factorial growth]{def="Candidate count that scales like n!n!."}.

Footnotes

  1. FreeCodeCamp: Brute Force Algorithms Explained - Explains brute-force solution for TSP by trying every possible route; notes inefficiency and gives context for brute-force complexity. ↩

How candidate counts explode for common brute-force patterns

Candidate counts are the main driver of brute-force runtime; verification per candidate may add a factor.

Where brute force fits in the algorithm design landscape

Common algorithm design paradigms include divide and conquer, dynamic programming, greedy, backtracking/branch-and-bound, etc. Brute force is a baseline that tests all candidates without structural pruning. Algorithm strategy material often lists brute force as one of the core design techniques alongside others.

A practical guideline: use brute force

  • as a correctness baseline (verify results of faster algorithms),
  • on small inputs, or
  • when no better structure is available. A general discussion of choosing techniques notes brute force is correct but slow due to testing all possible solutions.

Key term set: algorithm paradigm, correctness baseline, structural pruning.

Footnotes

  1. ALGORITHMS DESIGN STRATEGIES (Chapter 5 PDF) - Provides a classification of algorithm design strategies; describes brute force as iterating through a domain of solutions without shortcuts. ↩

  2. CloudThat Resources: Brute Force, Greedy, and Dynamic Programming (Key Differences) - Discusses when to use brute force (small inputs/baselines) and why it is correct but slow (tries all solutions). ↩

Pro Tip

When teaching or learning, always write (1) the candidate definition and (2) the verification predicate first. Those two choices determine whether your brute force is correct and how fast it will be.

Warning: Runtime blow-up

Brute force can become infeasible because candidate counts grow extremely fast (e.g., 2n2^n subsets or n!n! permutations). Even if each test is simple, the total work can explode, which is why brute force is often used on small inputs or for benchmarking.2

Footnotes

  1. Brute-force search - Wikipedia - Defines brute-force/exhaustive search as systematically checking all possible candidates; includes examples like divisors and eight queens and discusses candidate explosion. ↩

  2. FreeCodeCamp: Brute Force Algorithms Explained - Explains brute-force solution for TSP by trying every possible route; notes inefficiency and gives context for brute-force complexity. ↩

Common FAQs about brute force

Knowledge Check

Question 1 of 4
Q1Single choice

Brute-force search (exhaustive search) is best described as: