Algorithm defined
What is an Algorithm?
An algorithm is a precise, finite sequence of steps that can be followed to complete a task. Algorithms are not exclusive to computing - everyday activities such as following a recipe, giving someone directions, or sorting a pile of books all involve algorithmic thinking. What defines a well-formed algorithm is that it is unambiguous (every step has exactly one meaning), finite (it terminates after a defined number of steps), and effective (it correctly achieves its intended goal when followed).
Algorithms and Computer Programs
A common source of confusion is treating an algorithm and a computer program as the same thing. An algorithm is an abstract description of the steps needed to solve a problem - it exists independently of any programming language. A computer program is one specific implementation of that algorithm, written in a particular language so that a machine can execute it. The same algorithm for, say, sorting a list could be implemented in Python, Java, or any other language; the underlying logic remains identical.
Determining the Purpose of an Algorithm
When presented with an unfamiliar algorithm, use the following approach to identify its purpose:
- Identify the inputs - what data or values does the algorithm receive?
- Trace through each step in order, noting how the data changes.
- Identify the output or end result produced.
- State the overall goal clearly in plain English.
The three examples below each describe a simple algorithm using plain steps. Work out the purpose of each one before reading the explanation.
Algorithm steps:
- Set largest to the first number in the list.
- For each remaining number in the list:
- If the current number is greater than largest, set largest to that number.
- Output largest.
Purpose: This algorithm finds the largest value in a list of numbers. It works by assuming the first item is the largest, then updating that assumption whenever a bigger number is found. After examining every item, the final value of largest is the answer.
Algorithm steps:
- Set count to 0.
- Input a target value.
- For each item in the list:
- If the item equals target, add 1 to count.
- Output count.
Purpose: This algorithm counts how many times a specific value appears in a list. It scans every item and increments a counter each time a match is found, then outputs the final total.
Algorithm steps:
- Input a number from the user.
- If the number is greater than 0, output "Positive".
- Otherwise, if the number is less than 0, output "Negative".
- Otherwise, output "Zero".
Purpose: This algorithm classifies a number as positive, negative, or zero. It uses conditional logic to select the correct label based on the input value and outputs exactly one result. This demonstrates that not all algorithms produce a numerical result - the output here is a classification.
Key Takeaways
- An algorithm is a finite, unambiguous sequence of steps designed to complete a task.
- A computer program is an implementation of an algorithm - the algorithm itself is not a program.
- The same algorithm can be implemented in many different programming languages.
- To determine an algorithm's purpose: trace the inputs through each step and identify what output is produced.
- Algorithms appear in everyday life, not only in computing - anywhere a repeatable process exists, an algorithm can describe it.