What is an Algorithm?
This lesson walks through a practical definition that answers the question: What is an Algorithm? It includes examples on how algorithms are used in both real world and development scenarios.
Guide Tasks
  • Read Tutorial

An Algorithm in the Wild

Imagine that you are camping in the woods with a group of friends for a week. For some reason or another you've been tasked with chopping down a tree for firewood. In order to knock the tree down would you simply run at it and try to topple it over? I hope not (but if you do please make sure to video it and send me the link so I can watch). Most likely you'll come up with a strategy, maybe something like:

  1. Grab an axe
  2. Chop on both sides of the tree multiple times
  3. Get out of the way so the tree doesn't land on you

large

Believe it or not, we just walked through an algorithm. Albeit a real world example of an algorithm.

Algorithm Definition

So what exactly is an algorithm? The dead simple definition is:

An algorithm is a process to solve a problem.

Going back to our lumberjack example, let's break it down:

Problem: a tree had to be cut down.

Solution: follow steps in order to chop it down.

That's easy, right? Algorithms in computer science are the same. The only difference is that we, as computer scientists use code instead of an axe to accomplish our goals. Algorithms can be intimidating to newcomers, however if you ever start to feel lost when studying them remind yourself to go back to the basics: an algorithm is simply a process to solve a problem.

Computer Science Algorithm Example

large

With our real world example under our belts, while you're sitting at the campfire let's walk through a computer science algorithm. Let's say that we've been tasked with counting up all of the words in a document. This is similar to what Microsoft Word does. Let's walk through what the process would look like.

  1. Convert each word into an element in an array/list (arrays/lists are containers of data).
  2. Loop through the collection and count each element.
  3. Print out the total after all items have been counted.

See, that wasn't too bad. That's an algorithm for counting all of the words in a document, and it's fully functional. Here is some Python code that is fully functional that accomplishes the goal.

# Words stored in a variable
words = "My amazing document full of words"

# Convert words into an array/list, output is:
# ["My", "amazing", "document", "full", "of", "words"]
words = words.split()

# Set a counter
sum = 0

# Loop through the words collection
for word in words:
  sum += 1

# Print out the word count
print(sum)

Now that you can answer the question: What is an Algorithm? In the next guide we'll discuss why it's important to study algorithms.