Concepts / Algorithm Development

Algorithm Development

An algorithm is a clear sequence of steps for carrying out a task.

  • Programming

Two Words That Get Mixed Up

Beginners often use the words algorithm and program as if they meant the same thing. That is understandable, because both describe how a computer carries out a task. This article separates the two ideas, starting with the algorithm itself. An algorithm is a clear sequence of steps for carrying out a task, and the rest of this article traces what makes a sequence of steps count as one.

What do you think happens?

Suppose a procedure gives clear steps for a task but its last step sends control back to the first step, so it never stops. Does it count as an algorithm?

  • Yes, because every step is clear

  • No, because it never finishes

Reveal answer
Answer:

No, because it never finishes

Clear steps are only part of the definition. The procedure must also finish after a finite number of steps, so a procedure that may continue forever does not meet the technical definition of an algorithm.

What Makes a Procedure an Algorithm

Definition

An algorithm is a clear sequence of steps for carrying out a task. Its steps must be unambiguous, and the procedure must finish after a finite number of steps. Those two requirements, together with the step-by-step structure, make up the technical definition used in this course.

Tracing a Procedure That Never Stops

Consider two small procedures, both built from the same kinds of steps. The first repeatedly adds 1 to a counter and writes the counter, then returns to the start. Nothing in it ever leads to a stop step, so control keeps looping. The second adds 1 to a counter, checks whether the counter equals 3, and either repeats the addition or stops. The second procedure reaches a stop step after a finite number of passes, so it meets the requirement that the procedure must finish.

Add 1 to counterWrite counterAdd 1 to counterCounter equals 3?Stopnextloop backnextnoyes
Where does the control flow loop back with no exit path, and how does that differ from a procedure that reaches a stop step?

In the left-hand procedure there is no exit path, so the control flow circulates indefinitely. Because the procedure may continue forever, it does not meet the technical definition of an algorithm, even though each individual step is clear. The right-hand procedure has a decision that leads to a stop step, which is the kind of finite ending the definition requires.

Algorithm Versus Program

The algorithm is the step-by-step idea for solving the task. A program is not the same as an algorithm. It is an implementation of an algorithm in a particular programming language. The algorithm describes what the steps are, while the program expresses those same steps using the syntax and conventions of one specific language.

AspectAlgorithmProgram
Main ideaA clear step-by-step procedure for a taskAn implementation of that procedure
LanguageNot tied to any particular programming languageWritten in one particular programming language
Level of detailFocuses on the sequence of actionsIncludes the syntax the language requires
Must finishYes, after a finite number of stepsDepends on how the procedure was written

Comparing an algorithm with a program that implements it

Because an algorithm does not depend on a particular language, the same algorithm can be implemented in more than one programming language. Each implementation is a different program, but all of them carry out the same underlying sequence of steps.

Describing an Ordering Procedure in English

An algorithm can be written in English before any code exists. Consider the task of arranging three named items in alphabetical order. A possible algorithm in English is: compare the items, identify which belongs first, place it first, then compare the two remaining items and place the earlier one next. This description focuses on the sequence of actions rather than on the syntax of a programming language, which makes it an algorithmic description.

  1. 1
    Compare the items and identify which one belongs first in alphabetical order.
  2. 2
    Place that item in the first position.
  3. 3
    Compare the two remaining items and identify which belongs earlier.
  4. 4
    Place the earlier of the two remaining items in the second position.
  5. 5
    Place the last remaining item in the third position.
Worked example

Tracing the English algorithm on three names

Arrange the names Mango, Apple, and Cherry in alphabetical order using the English description above.

  1. 1
    Compare and identify the first item

    Comparing the three names shows that Apple belongs first alphabetically, so Apple is placed in the first position.

  2. 2
    Compare the remaining two

    The two remaining names are Mango and Cherry. Comparing them shows that Cherry belongs before Mango.

  3. 3
    Place the earlier remaining item

    Cherry is placed in the second position, which leaves Mango for the third position.

Result

Apple, Cherry, Mango

The English description is already an algorithm, because every step is clear and the procedure finishes after a finite number of steps. A programmer would later turn each step into code in a chosen language. Writing the code adds syntax and implementation details, but it does not change the underlying sequence of steps.

Mistakes Learners Make

  • Common mistake

    Treating algorithm and program as the same thing

    What it looks like

    Saying that the Python code is the algorithm for sorting names.

    Why it fails

    The algorithm is the step-by-step idea, while a program is that idea expressed in a particular programming language.

    Do this instead

    Describe the steps first in plain language, then recognize that a program is one implementation of those steps.

  • Common mistake

    Assuming that clear steps are enough

    What it looks like

    Calling a procedure an algorithm because each of its steps is easy to understand, even though it loops back and never stops.

    Why it fails

    The definition also requires that the procedure must finish after a finite number of steps.

    Do this instead

    Check both clarity and termination. Confirm that some sequence of steps reaches an end.

  • Common mistake

    Waiting for code before describing the procedure

    Why it fails

    An algorithm can be expressed in English before all implementation details are turned into code.

    Do this instead

    Write the steps in plain English first, checking that each step is unambiguous, and then refine them into code.

Practice Deciding What Counts

MEDIUM

Write a short English description of an algorithm for finding the largest of three numbers. Then check your description against the definition: are all steps unambiguous, and does the procedure finish after a finite number of steps? Finally, state whether the same steps would still be an algorithm if you later wrote them in a different programming language.

Hints
  • Describe the actions in order, such as comparing two numbers and keeping the larger one.
  • Ask whether any step could be interpreted in more than one way.
  • Consider whether the description depends on any specific programming language's syntax.

Key Takeaways

  • An algorithm is a clear sequence of steps for carrying out a task, and its steps must be unambiguous.
  • The procedure must finish after a finite number of steps, so a procedure that may continue forever does not meet the technical definition of an algorithm.
  • A program is an implementation of an algorithm in a particular programming language, so one algorithm can be implemented in several languages.
  • An algorithm can be described in English, focusing on the sequence of actions, before any implementation details are turned into code.