Implementing algorithms
Once a formal description has been obtained, an algorithm is a well-defined method or procedure for solving a problem such as a problem in mathematics; or otherwise relating to the manipulation of information.
Algorithms are now most often implemented as computer programs but can be implemented by other means, such as electric circuits or a machine. They may even be performed directly by humans: think for example of an abacus, or performing arithmetic with pen and paper or its mental equivalent - most people use algorithms they learned as a child to do this.
The analysis and study of algorithms is a central discipline of computer science, and is often practiced abstractly (without the use of a specific programming language, designed for practical implementation). In this sense, it resembles other mathematical disciplines in that the analysis focuses on the underlying principles driving the algorithm, and not it's particular implementation. The 'coding' (more properly 'codifying' though this terminology is seldom, if ever, used) of algorithms in such an abstract manner is termed 'writing pseudocode'.
Some restrict the definition of algorithm to procedures that eventually finish. Others include procedures that could run forever without stopping, arguing that a computer may be required to carry out an ongoing task. Other requirements beside having an ending state, then, are used to determine if the algorithm successfully completes a task.
Example
Here is a simple example of an algorithm.
Imagine you have an unsorted list of random numbers. Our goal is to find the highest number in this list. Upon first thinking about the solution, you will realize that you must look at every number in the list. Upon further thinking, you will realize that you need to look at each number only once. Taking this into account, here is a simple algorithm to accomplish this:
- Pretend the first number in the list is the largest number.
- Look at the next number, and compare it with this largest number
- Only if this next number is larger, then keep that as the new largest number
- Repeat steps 2 and 3 until you have gone through the whole list.
And here is a more formal coding of the algorithm in a pseudocode that is similar to most programming languages:
Given: a list List of length Length
counter = 1
largest = List[counter]
while counter <= Length:
if List[counter] > largest:
largest = List[counter]
counter = counter + 1
print largest
Notes on notation:
- = as used here indicates assignment. That is, the value on the right-hand side of the expression is assigned to the container (or variable) on the left-hand side of the expression.
- List[counter] as used here indicates the counterth element of the list. For example: if the value of counter is 5, then list[counter], refers to the 5th element of the list.
- <= as used here indicates 'less than or equal to'
As it happens, most people who implement algorithms want to know how much of a particular resource (such as time or storage) a given algorithm requires. Methods have been developed for the