What is an algorithm?

    An algorithm is a sequence of fundamental instructions, which when carried out, accomplish some larger task.  For example, this is an algorithm which you might use to eat a banana:

  1. Walk to refrigerator
  2. Open refrigerator door
  3. Locate banana
  4. Take banana
  5. Close refrigerator door
  6. Peel banana
  7. Eat banana
This is a very simple algorithm in that the instructions are all carried out in order, and no instruction is repeated.  Algorithms can also have looping structures that allow for repetitive or incremental behavior, as well as conditional instructions that are only carried out under certain circumstances.  Consider the following algorithm to eat a bowl of cereal:
  1. Open cereal box
  2. Pour cereal into bowl
  3. Close cereal box
  4. Pour milk into bowl
  5. Pick up spoon
  6. Repeat the following instructions until the bowl is empty
    1. Place spoon in milk
    2. Scoop a spoonful of cereal
    3. Eat the spoonful of cereal
  7. If face is wet then
    1. Pick up napkin
    2. Wipe face with napkin
    3. Put down napkin
Step 6 is an example of a looping instruction, while step 7 is an example of a conditional instruction.

    One purpose of an algorithm is to break a larger task down so that each step contains a fundamental instruction that can be carried out without any creativity.  For example, a human could accomplish the above tasks without such a list of instructions.  However, a robot may come with a very limited set of instructions it understands, such as WALK FORWARD, TURN RIGHT, RAISE ARM, CLOSE HAND.  If a robot attempted to accomplish the above tasks, then you would need to break down the task into these fundamental commands.

  A real computer algorithm might do something like adding up a bunch of numbers.

  1. Repeat the following instructions until there are no numbers left in the list
    1. Read a number off the list, let's call that number n
    2. Add n to the value in an accumulator
  2. Print out the total in the accumulator