Algorithm Development
An algorithm is a clear sequence of steps for carrying out a task.
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?
Reveal 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
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.
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.
| Aspect | Algorithm | Program |
|---|---|---|
| Main idea | A clear step-by-step procedure for a task | An implementation of that procedure |
| Language | Not tied to any particular programming language | Written in one particular programming language |
| Level of detail | Focuses on the sequence of actions | Includes the syntax the language requires |
| Must finish | Yes, after a finite number of steps | Depends 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.
- 1Compare the items and identify which one belongs first in alphabetical order.
- 2Place that item in the first position.
- 3Compare the two remaining items and identify which belongs earlier.
- 4Place the earlier of the two remaining items in the second position.
- 5Place the last remaining item in the third position.
Tracing the English algorithm on three names
Arrange the names Mango, Apple, and Cherry in alphabetical order using the English description above.
- 1Compare and identify the first item
Comparing the three names shows that Apple belongs first alphabetically, so Apple is placed in the first position.
- 2Compare the remaining two
The two remaining names are Mango and Cherry. Comparing them shows that Cherry belongs before Mango.
- 3Place the earlier remaining item
Cherry is placed in the second position, which leaves Mango for the third position.
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 likeSaying that the Python code is the algorithm for sorting names.
Why it failsThe algorithm is the step-by-step idea, while a program is that idea expressed in a particular programming language.
Do this insteadDescribe 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 likeCalling a procedure an algorithm because each of its steps is easy to understand, even though it loops back and never stops.
Why it failsThe definition also requires that the procedure must finish after a finite number of steps.
Do this insteadCheck both clarity and termination. Confirm that some sequence of steps reaches an end.
Common mistake Waiting for code before describing the procedure
Why it failsAn algorithm can be expressed in English before all implementation details are turned into code.
Do this insteadWrite the steps in plain English first, checking that each step is unambiguous, and then refine them into code.
Practice Deciding What Counts
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.