Brute Force and Algorithm Types

The 7 Main Types of Algorithms and How They Work

Seven common algorithm types, from brute force and greedy to dynamic programming and randomized methods, with examples of how each one works.

On this page
  1. Seven common types
  2. Brute force
  3. Divide and conquer
  4. Dynamic programming
  5. Greedy
  6. Backtracking
  7. Branch and bound
  8. Randomized
  9. Choosing between them

Seven common types

There is no single official list of algorithm types. Textbooks and course notes differ, and some add a category for simple recursive algorithms. The list below covers seven types that appear in most of them: brute force, divide and conquer, dynamic programming, greedy, backtracking, branch and bound, and randomized. Each is a general strategy for solving problems, not a specific algorithm.

Brute force

Brute force tries every possible candidate and checks each one against the goal. Linear search, which checks a list item by item, is the simplest example. It is always correct when the answer is among the candidates, but the number of candidates often grows too fast for large inputs. For more on this approach, see brute force algorithm examples.

Divide and conquer

Divide and conquer splits a problem into smaller pieces of the same kind, solves each piece, and then combines the results. Merge sort is a standard example: it splits a list in half, sorts each half, and merges them. Quicksort works in a similar way. Divide and conquer methods are often written as recursion, where a function calls itself on smaller inputs. The recursive algorithm examples article covers that style in more detail.

Dynamic programming

Dynamic programming solves a problem by storing answers to smaller parts and reusing them, rather than recalculating them each time. It is mostly used for optimization problems, where the goal is the best answer, and it works when smaller parts overlap. Computing Fibonacci numbers is a common teaching case: a naive recursive version recalculates the same values again and again, while a stored table of results takes one step per number.

For the coins worth 1, 3 and 4 cents from the greedy example, dynamic programming finds the best answer of two 3-cent coins, where a greedy method takes three coins.

Greedy

A greedy method makes the best-looking choice at each step and never reconsiders. It is fast and simple, and it gives the best answer for some problems. Standard US coins are one case, and minimum spanning trees are another. For other problems, a greedy choice can miss the best answer, so a greedy method has to be checked against the problem before it is trusted.

Backtracking

Backtracking explores choices one at a time, going deeper into one path. When a path reaches a dead end, it undoes the last choice and tries another. Solving a sudoku puzzle and placing queens on a chessboard so that none attack each other are classic examples. Backtracking is often much faster than brute force, because it abandons a whole path as soon as it cannot work.

Branch and bound

Branch and bound builds a search tree of choices and skips any branch that cannot beat the best answer found so far. The traveling salesman problem is a typical example. Branch and bound is still a search method, so its speed depends on how well it can prune the tree. It is often used when the best answer is needed and the problem is too large for plain brute force.

Randomized

A randomized algorithm makes some of its choices at random. In quicksort, picking the pivot at random is a common approach, because it makes very slow cases unlikely on typical inputs. Randomized methods don’t always give the same path or the same running time for the same input, but they are often simple and fast in practice.

Choosing between them

No single type is best for every problem. Brute force is easy to trust on small inputs. Greedy and dynamic programming are fast for problems that suit them, with dynamic programming more reliable when greedy choices can go wrong. Backtracking and branch and bound handle hard search problems by pruning, and divide and conquer and randomized methods are often used for sorting and searching. Knowing the type helps you guess what to try first on a new problem.

The complete guide to brute force and algorithm types

Brute Force Algorithm Examples
Read the guide

Keep exploring

Guides from other topics

All topics →