What is an algorithm?

Free preview 4 min to read

Algorithms are everywhere

An algorithm is a precise, finite sequence of well-defined steps that solves a specific problem or performs a specific task.

You already use algorithms every day without calling them that:

  • A recipe is an algorithm: ingredients (input), steps (process), dish (output)
  • Driving directions are an algorithm: start point, turn-by-turn instructions, destination
  • Looking up a word in a physical dictionary: open to the middle, compare, go left or right: that's binary search!

An algorithm is not code. Code is merely one way to express an algorithm.


The three properties

For something to qualify as an algorithm, it must have three properties:

1. Finiteness

It must terminate after a finite number of steps. An infinite loop is not an algorithm: it's a bug.

# This is NOT an algorithm — it never stops
WHILE true
  PRINT "hello"
END WHILE

# This IS an algorithm — it stops after 5 iterations
count ← 0
WHILE count < 5
  PRINT "hello"
  count ← count + 1
END WHILE

2. Definiteness

Each step must be unambiguous. There should be one interpretation.

# BAD: ambiguous
"Add some salt"       → How much is "some"?

# GOOD: definite
"Add 5 grams of salt" → Exactly one interpretation

3. Effectiveness

Each step must be executable: something that can be carried out.

# BAD: not effective
"Find the meaning of life"  → Cannot be computed

# GOOD: effective
"Find the largest number in the array"  → Clear, executable

Input, process, output

Every algorithm follows the same fundamental structure:

flowchart LR
  I["Input<br/>(data)"] --> P["Process<br/>(steps)"] --> O["Output<br/>(result)"]

Example: finding the maximum value

  • Input: A list of numbers [42, 17, 93, 5, 68]
  • Process: Compare each number, track the largest seen so far
  • Output: 93
FUNCTION find_max(numbers)
  max ← first element of numbers

  FOR EACH number IN numbers
    IF number > max
      max ← number
    END IF
  END FOR

  RETURN max
END FUNCTION

This algorithm has all three properties:

  • Finite: It processes each element once, then stops
  • Definite: Each comparison has one clear outcome
  • Effective: Comparing two numbers is a basic operation any computer can do

Algorithms in Rails

Rails is full of algorithms working behind the scenes:

Operation Algorithm at Work
User.find(42) Search algorithm (index lookup)
Course.order(:position) Sorting algorithm (database sort)
params.permit(:name, :email) Filtering algorithm (whitelist)
has_secure_password Hashing algorithm (bcrypt)
Route matching Pattern matching algorithm

When you write User.find(42), PostgreSQL uses a B-tree index to locate the row: a sophisticated search algorithm that finds any record among millions in just a few steps.


Why study algorithms?

You might think: "Rails does everything for me: why learn algorithms?"

Because understanding algorithms helps you:

  1. Choose the right approach: Should you use find or where? each or find_each? The answer depends on the underlying algorithm.
  2. Debug performance issues: "Why is this page slow?" often comes down to an O(n²) algorithm hiding in your code.
  3. Design better features: Recommendation engines, search features, and ranking systems all require algorithmic thinking.
  4. Pass technical interviews: Many Rails job interviews include algorithm questions.

Algorithms vs. heuristics

Not every problem has an algorithm that guarantees the perfect answer:

Algorithm Heuristic
Guarantee Always correct Usually good enough
Speed Can be slow for perfect results Fast, trades accuracy for speed
Example Checking every route to find the shortest "Always go towards the destination"
Rails example Course.order(:position) "Show most popular first" (approximation)

Knowing when you need a precise algorithm vs. a good-enough heuristic is a professional skill.


Summary

Concept Definition
Algorithm Finite, definite, effective sequence of steps
Finiteness Must terminate
Definiteness Each step is unambiguous
Effectiveness Each step is executable
Structure Input → Process → Output
Heuristic A "good enough" shortcut when perfect answers are too expensive
Lesson 1 of 7 · free

Keep going with lesson 2

Pseudocode fundamentals

Presents pseudocode conventions, the anatomy of a function, four common patterns and a translation table to Ruby.

A free account also gives you the quizzes, your progress and notes, and the discussion.

Type to start searching...