Glimmer Labs

The Parts of an Algorithm

As you may have noted, there are some common aspects to algorithms. That is, there are techniques that we use in many of the algorithms we write. It is worthwhile to think about these algorithm parts because we can rely on them when we write new algorithms. Common parts of algorithms include

Variables: Named Values

As we write algorithms, we like to name things. Sometimes we use long names, such as the piece of bread in your left hand. Sometimes, we use shorter names, such as bread-left. As we start to write more formal algorithms, we may explicitly say that we're using names.

Parameters: Named Inputs

Many algorithms work on data that are presented to the algorithm. For example, our PBJ algorithm should work no matter what kind of bread or peanut butter we give to the algorithm. That algorithm also requires that we give it bread and peanut butter. Similarly, an algorithm to find a name in the phone book might take the name and the phone book as input values. We call such input values parameters.

Conditionals: Handling Different Conditions

At times, our algorithms have to account for different conditions, doing different things depending on those conditions. In our PBJ algorithm, we might check whether the jar of peanut butter is open or what kind of lid is on the jelly jar. We call such operations conditionals. They typically take either the form

if some condition holds then do something

of the more complex form

if some condition holds then do something otherwise do something else

At times, we need to decide between more than two possibilities.

Repetition

At times, our algorithms require us to do something again and again. In our PBJ algorithm, we may have had to turn the twisty-tie again and again until it was untwisted. In our many-sandwiches algorithm, we made one sandwich again and again. We call this technique repetition. Repetition takes many forms. We might do work until we've reached a desired state.

repeat some action until some condition holds

We might continue work as long as we're in some state.

repeat some action while some condition holds

We might repeat an action a fixed number of times.

repeat some action N times

You can probably think of other forms of repetition.

Subroutines: Named Helper Algorithms

Many algorithms require common actions for their operation. For example, to make N sandwiches, you benefit from knowing how to make one sandwich. To make a peanut butter and jelly sandwich, it helps to know how to spread something on bread. We can write additional algorithms for these common actions and use them as part of our broader algorithm. We can also use them in other algorithms. We call these helper algorithms subroutines.

Recursion: Helping Yourself

As strange as it may seem, we sometimes find it useful to define an algorithm in terms of itself. In particular, we can have an algorithm deal with a large or complex input by using itself as a helper with a smaller or less complex input.

Consider the following example

To make N peanut butter and jelly sandwiches
  If N is 0 then             conditional
    Celebrate, you're done!
  Otherwise
    Make one PBJ sandwich    subroutine
    Make N-1 PBJ sandwiches  recursion

We call this technique recursion. Mastering recursion is one of the key steps on your path to becoming a computer scientist.

A helpful way to think about recursion: Every time you use a subroutine you hand the input to that algorithm and the instructions for the algorithm to a friend. That friend executes the algorithm and hands the result back to you. It shouldn't matter whether the algorithm you give your friend is a different one than you're using or the same one.


This document was generated by Siteweaver on Tue Aug 17 07:24:49 2010.
The source to the document was last modified on Thu Aug 29 20:57:16 2002.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Glimmer/Sorting/algorithm-parts.html.

You may wish to validate this document's HTML ; Valid CSS! ; Check with Bobby

Glimmer Labs: The Grinnell Laboratory for Interactive Multimedia Experimentation & Research
glimmer@grinnell.edu