Algorithm Design Techniques – Brute Force Approach with Examples

Introduction

Designing an algorithm becomes easier when you know the core design techniques. Algorithm design paradigms such as Brute Force, Divide and Conquer, Greedy Method, Dynamic Programming, Decrease and Conquer, and Backtracking provide systematic strategies for solving computational problems efficiently.
This lecture introduces the Brute Force technique, the simplest yet fundamental method for algorithmic problem solving.

Table of Contents

  1. What Are Algorithm Design Techniques?

  2. Why Study Algorithm Design Strategies?

  3. Common Algorithm Design Paradigms

  4. Brute Force Design Technique

    • Definition and Concept

    • When to Apply Brute Force

    • Strengths and Weaknesses

  5. Examples of Brute Force Algorithms

    • Example 1 – Swapping Two Variables

    • Example 2 – Computing xⁿ

    • Example 3 – Factorial of n

    • Example 4 – Decimal to Binary Conversion

    • Example 5 – Counting Binary Digits

    • Example 6 – Finding Maximum in Array

    • Example 7 – Matrix Multiplication

    • Example 8 – Sequential Search

    • Example 9 – Polynomial Evaluation

    • Example 10 – Converting 2D Array to 1D

  6. Where to Use the Brute Force Method

  7. Conclusion

What Are Algorithm Design Techniques?

An algorithm design technique (or strategy) is a general approach for solving a range of computational problems.
Each technique offers a systematic framework for problem-solving and is applicable across multiple domains, including data structures, AI, optimization, and numerical computation.

Why Study Algorithm Design Strategies?

  • Provide guidance in developing algorithms for new problems.

  • Offer reusable frameworks applicable to a wide range of problems.

  • Help identify patterns and efficiency improvements.

  • Simplify complex problems through structured thinking.

Common Algorithm Design Paradigms

  1. Brute Force

  2. Decrease and Conquer

  3. Divide and Conquer

  4. Greedy Technique

  5. Dynamic Programming

  6. Backtracking

Brute Force Design Technique

Definition and Concept

Brute Force is a straightforward approach that directly implements the problem’s definition without optimization.
It systematically explores all possible solutions until a valid one is found.

Also known as:

  • Generate and Test

  • Exhaustive Search

In short: “Just do it!”

When to Apply Brute Force

  • For small problem sizes where efficiency is less important.

  • When developing a baseline algorithm before optimization.

  • For numerical, searching, or sorting problems where logic is simple.

  • As a verification tool to test correctness of optimized solutions.

Strengths and Weaknesses

Strengths

  • Easy to design and understand.

  • Universally applicable to many problems.

  • Good starting point for optimization.

 Weaknesses

  • Often inefficient for large inputs.

  • Can lead to exponential growth in computation.

  • Not suitable for real-time or large-scale systems.

Examples of Brute Force Algorithms

Example 1 – Swapping Two Variables

ALGORITHM Swap(x, y)
temp ← x
x ← y
y ← temp
return (x, y)

Simple brute-force solution using a temporary variable

Example 2 – Computing xⁿ

ALGORITHM Power(x, n)
answer ← 1
i ← 1
while (i ≤ n)
answer ← answer * x
i ← i + 1
return answer

Brute force approach: multiply x by itself n times.

Example 3 – Factorial of n

ALGORITHM Factorial(n)
fact ← 1
i ← 1
while (i ≤ n)
fact ← fact * i
i ← i + 1
return fact

Time complexity: O(n)

Example 4 – Decimal to Binary Conversion

ALGORITHM Binary(n)
while (n > 0)
b[k] ← n mod 2
n ← n / 2
return b[]

Repeated division by 2 — a typical brute-force numeric conversion.

Example 5 – Counting Binary Digits

ALGORITHM CountBits(n)
count ← 1
while (n > 1)
count ← count + 1
n ← n / 2
return count

Counts the number of bits in a binary number.

Example 6 – Finding Maximum in an Array

ALGORITHM MaxElement(A[0..n−1])
max ← A[0]
for i ← 1 to n−1
if (A[i] > max)
max ← A[i]
return max

Checks each element — pure brute force.

Example 7 – Matrix Multiplication

ALGORITHM MatrixMultiply(A, B)
for i ← 0 to n−1
for j ← 0 to n−1
C[i][j] ← 0
for k ← 0 to n−1
C[i][j] ← C[i][j] + A[i][k] * B[k][j]
return C

Classic brute-force nested-loop multiplication (O(n³)).

Example 8 – Sequential Search

ALGORITHM SequentialSearch(A[0..n−1], key)
i ← 0
while (i < n and A[i] ≠ key)
i ← i + 1
if (i < n)
return i
else
return −1

Linear search – the simplest brute-force searching algorithm.

Example 9 – Polynomial Evaluation

p(x)=anxn+an1xn1+...+a1x+a0p(x) = a_nx^n + a_{n−1}x^{n−1} + … + a_1x + a_0

ALGORITHM PolynomialEval(A[0..n], x)
p ← 0
for i ← n downto 0
power ← 1
for j ← 1 to i
power ← power * x
p ← p + A[i] * power
return p

Improved version uses Horner’s rule for efficiency.

Example 10 – Converting 2D Array to 1D

ALGORITHM Convert2DTo1D(arr2D[n][m], arr1D[n*m])
for i ← 0 to n−1
for j ← 0 to m−1
arr1D[i*m + j] ← arr2D[i][j]
return arr1D

Maps 2D indices into linear 1D representation.

Where to Use the Brute Force Method

  • For small problem sizes.

  • As a starting point to understand logic before optimizing.

  • In testing, debugging, or prototype design.

  • For exhaustive search problems like puzzles, password generation, and combinatorial search.

Conclusion

The Brute Force technique is simple yet powerful for understanding algorithm design fundamentals. It builds the foundation for advanced paradigms like Divide and Conquer, Greedy, and Dynamic Programming. Although inefficient for large-scale problems, brute force provides valuable insight into problem-solving logic and algorithm structure. Complete lecture Lecture # 13_new

Search within CuiTutorial

Scroll to Top