Section 1.2 Algorithms
If problem solving is a central part of computer science, then the solutions that you create through the problem solving process are also important. In computer science, we refer to these solutions as algorithms. An algorithm is a step by step list of instructions that if followed exactly will solve the problem under consideration.
For example, an algorithm to compute the length of a triangle’s hypotenuse might look like this:
Notice that this algorithm consists of a set of steps. It is written in English, for ease of understanding but that it assumes the reader knows the required sub-steps. Although simple algorithms are easily understood when written in English, more complicated algorithms need more precise notation. For improved precision, algorithms are often written in pseudocode. Pseudocode is a notation that is more precise than English but generally not as precise as a programming language. The same algorithm expressed in pseudocode might look something like this:
Notice how the pseudocode example is more structured - using numbered steps and a hierarchy of sub-steps as needed; and how it uses variables and a formula to express the mathematics more precisely.
Our goal in computer science is to take a problem and develop an algorithm that can serve as a general solution: a solution that can work in (most) circumstances. Once we have such a solution, we can use our computer to automate its execution. Programming is a skill that allows a computer scientist to take an algorithm and represent it in a notation (a program) that can be followed by a computer. A program is written in a programming language such as Python, the language you will learn in this book.
To help you understand the differences amongst an algorithm, psuedocode and a program, consider this program that finds the hypothenuse of a right-angled triangle:
A program is an algorithm expressed in a programming language. We might also say that a program is an implementation of an algorithm. In this example, both the algorithm and the program have three major steps. The first step gets some input from the user and the input into something the computer can do math with; the second step performs a calculation using the information obtained in the first step; and the final step displays the result to the user. Even though we haven’t covered any details of Python, hopefully you can see the correspondence between the steps of the algorithm, which could be followed by a human (but not executed by a computer), and the steps of the program, which can be executed by a computer (try executing this one using the Run button).
Algorithms are important because the process of solving a problem through programming begins with designing an algorithm. The programmer often expresses the algorithm in pseudocode to better understand the sub-steps, then converts the algorithm to a program for the computer to execute. In the next section, you will learn how to execute Python programs on a computer.
Check your understanding
Checkpoint 1.2.3.
An algorithm is:
A solution to a problem that can be solved by a computer.
While it is true that algorithms often do solve problems, this is not the best answer. An algorithm is more than just the solution to the problem for a computer. An algorithm can be used to solve all sorts of problems, including those that have nothing to do with computers.
A step by step sequence of instructions that if followed exactly will solve the problem under consideration.
Algorithms are like recipes: they must be followed exactly, they must be clear and unambiguous, and they must end.
A series of instructions implemented in a programming language.
Programming languages are used to express algorithms, but an algorithm does not have to be expressed in terms of a programming language.
A special kind of notation used by programmers.
Programmers sometimes use a special notation to illustrate or document an algorithm, but this is not the definition of an algorithm.
You have attempted
of
activities on this page.