Algorithms
Design Patterns
Programming
Software Development
Computer Science

What's the difference between an Algorithm and a Design Pattern

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Algorithms and design patterns are both reusable ideas in software, but they solve different kinds of problems. An algorithm tells you how to compute something step by step, while a design pattern tells you how to organize code so components collaborate in a maintainable way.

Core Sections

An algorithm is about computation

An algorithm is a finite procedure that takes input, performs a sequence of operations, and produces output. You usually evaluate an algorithm by asking questions such as:

  • is it correct
  • how fast is it
  • how much memory does it use

Binary search is a classic example.

python
1def binary_search(values: list[int], target: int) -> int:
2    left, right = 0, len(values) - 1
3    while left <= right:
4        mid = (left + right) // 2
5        if values[mid] == target:
6            return mid
7        if values[mid] < target:
8            left = mid + 1
9        else:
10            right = mid - 1
11    return -1
12
13
14print(binary_search([1, 3, 5, 7, 9], 7))

That code solves a direct computational problem: find the target efficiently in a sorted list.

A design pattern is about structure

A design pattern is a reusable solution template for a recurring software design problem. It is not primarily about numeric efficiency. It is about flexibility, decoupling, readability, and change management.

The Strategy pattern is a good example.

python
1from abc import ABC, abstractmethod
2
3class ShippingStrategy(ABC):
4    @abstractmethod
5    def cost(self, weight_kg: float) -> float:
6        raise NotImplementedError
7
8class GroundShipping(ShippingStrategy):
9    def cost(self, weight_kg: float) -> float:
10        return 5.0 + 0.8 * weight_kg
11
12class ExpressShipping(ShippingStrategy):
13    def cost(self, weight_kg: float) -> float:
14        return 12.0 + 1.2 * weight_kg
15
16class CheckoutService:
17    def __init__(self, strategy: ShippingStrategy) -> None:
18        self.strategy = strategy
19
20    def shipping_cost(self, weight_kg: float) -> float:
21        return self.strategy.cost(weight_kg)

The interesting part here is not the math. It is that shipping policy can change without rewriting the checkout flow. That is a structural design benefit.

They are evaluated by different criteria

Because they solve different problems, they should not be judged the same way.

Algorithms are often compared by:

  • time complexity
  • space complexity
  • numerical correctness

Design patterns are often compared by:

  • coupling and cohesion
  • extensibility
  • clarity of responsibilities
  • ease of testing and change

A pattern can be excellent even if it says nothing about asymptotic complexity. An algorithm can be excellent even if it lives in one small function and has no architectural significance at all.

Patterns often host interchangeable algorithms

The two ideas frequently work together rather than compete. A pattern may provide the structure that allows multiple algorithms to be swapped in.

python
1from abc import ABC, abstractmethod
2
3class SearchStrategy(ABC):
4    @abstractmethod
5    def find(self, values: list[int], target: int) -> int:
6        raise NotImplementedError
7
8class LinearSearch(SearchStrategy):
9    def find(self, values: list[int], target: int) -> int:
10        for index, value in enumerate(values):
11            if value == target:
12                return index
13        return -1
14
15class BinarySearch(SearchStrategy):
16    def find(self, values: list[int], target: int) -> int:
17        left, right = 0, len(values) - 1
18        while left <= right:
19            mid = (left + right) // 2
20            if values[mid] == target:
21                return mid
22            if values[mid] < target:
23                left = mid + 1
24            else:
25                right = mid - 1
26        return -1

Here, linear search and binary search are algorithms. The Strategy-style interface is the design pattern that makes them interchangeable.

A practical decision rule

In design discussions, ask a simple question:

  • are we deciding how to compute a result efficiently
  • or are we deciding how code units should collaborate over time

If the first question dominates, you are in algorithm territory. If the second dominates, you are in design-pattern territory.

That distinction helps prevent two common mistakes:

  • discussing architecture when the real issue is algorithmic efficiency
  • adding patterns when the code only needs a direct implementation of a computation

Common Pitfalls

  • Calling any reusable piece of code a design pattern blurs a useful distinction and weakens architectural discussion.
  • Evaluating design patterns mainly by big-O complexity misses the real reason patterns exist.
  • Ignoring algorithmic complexity because the architecture looks elegant can still leave the system too slow.
  • Applying patterns too early to tiny problems can create unnecessary abstraction.
  • Treating pattern names as checkboxes instead of responses to specific design pressures leads to cargo-cult design.

Summary

  • An algorithm is a step-by-step computational procedure.
  • A design pattern is a reusable structural solution for recurring design problems.
  • Algorithms are judged mainly by correctness and efficiency.
  • Design patterns are judged mainly by maintainability, flexibility, and clarity.
  • Good software often uses both: patterns for structure and algorithms for the actual computation.

Course illustration
Course illustration

All Rights Reserved.