Algorithms: give the computer instructions


Keywords: algorithms, flowcharts, pseudocode, work description

Algorithm programming Java Python

This article will demonstrate what an algorithm is and how to use them in computer programming. We will also see how we use both flowcharts and pseudocode to illustrate and structure an algorithm.

What is an algorithm?

An algorithm is a methodical and systematic description of how to solve a problem. The definition of an algorithm is a step-by-step procedure of well-defined executable instructions designed to perform a task or solve a problem. An algorithm has many similarities with a program where the difference is that we can describe algorithms regardless of program language.

The algorithm can be seen as a job description of how we should solve a problem and describe the steps the program should take to accomplish that. The critical thing to remember is that there are usually many algorithms for solving the same problem. It is often important to contemplate the simplest and most effective way to solve the problem, rather than only going with the first solution that works.

In mathematics and computer science, an algorithm is a finite sequence of well-defined, computer-implementable instructions, typically to solve a class of problems or to perform a computation

Wikipedia

With straightforward problem solving, you can manage by having your algorithm in your head, but as the degree of difficulty and complexity of the tasks increases, it is almost necessary to think through your algorithm carefully before you start coding. A good tip is to get in the habit of drawing or writing down the task’s solution. Therefore, you get an overview of your problem and can plan for each step of the way to your solution more easily.

Flowchart and pseudocode: tools for presenting an algorithm

At first, when creating a new algorithm, it can be complicated to get an idea of how and where to start and how everything is related. Setting up an algorithm is a creative process that requires problem-solving ability and structure. To help us make it easier to grasp the problem in front of us, we can use flowcharts and pseudocode. These techniques are not only beneficial to use for your benefit but also if you want to explain and discuss your intended solution with, for example, a friend or a colleague.

Flowcharts: structure your code

To visualize algorithms, you usually draw a so-called flowchart that shows what kind of action the program should perform. The three most common figures in a flowchart are:

Algorithms programming Java

Figure 1: Components in flowcharts
  • Start/Stop: shows where the algorithm starts and ends, in other words, it’s delimitations.
  • Activity: the steps performed in the process, for example, “turn on the light”, “start the engine” and so on.
  • Condition: choices and decisions in the algorithm, are usually designed as a yes / no question

If you want to draw your flow charts, we recommend draw.io, a free online site for drawing different figures and diagrams. We have used draw.io to illustrate the images we use in the examples on code-knwoledge.

Draw io Logo
Figure 2: draw io logo

Pseudocode: Plan your program before coding it

Pseudocode is in computer programming a program code-like text used to describe how the algorithm should be formed. We could describe pseudocode as a “code sketch“, often written in bullet point, on how an algorithm is structured. Related to flow charts that we saw before, pseudocode can be described as a flow chart in text form.

There is no general standard for pseudocode; instead, the important thing is to be consistent with words and format used. In pseudocode, details that are not critical to the understanding of the algorithm are often omitted. Instead, the pseudocode focus on describing the algorithm generally. It is also essential that you do not try to make it more complicated than it needs to be, which may be easier said than done. A good rule of thumb might be to think that the pseudocode, like the flowchart, is there to help you, not make it more complicated. The simpler and easier they are to read, the greater the chance you will write the correct code from the beginning.

There is no general standard for pseudocode; instead, the important thing is to be consistent with words and format used

Examples: Algorithms

Below we will see two examples of how an algorithm works. We will, of course, use the two tools we have discussed, flow charts and pseudocode. Let’s start with an example of an algorithm for a thermometer.

Example 1: Flow chart for a thermometer

In this example, we will draw an algorithm for a thermometer. We want to create a program that starts a heating element if the temperature is below 10 degrees. However, if the temperature is above 10 degrees, nothing should happen to the heating element, and the program should then wait 30 minutes before checking the temperature again.

If we write it down with a pseudocode, we get:

  1. Start thermometer: The algorithm starts with us starting the thermometer.
  2. Read the thermometer: We read the thermometer to see what the temperature is.
  3. Below 10 degrees? Is it below 10 degrees? If yes, start the element and exit the algorithm.
  4. Wait 30 minutes: If not, start a 30-minute timer and then start over in step 2.

Additionally, if we then use a flow chart to visualise it:

Example Algorithm computer programming

Figure 3: Example of an algorithm illustrated with a flow chart

Example 2: Algorithm to start the windscreen wipers

Let’s look at another example. We will write an algorithm that, depending on whether it rains and how much rain, will start the windshield wipers on a car. To help us, we have a rain sensor that measures how much it is raining.

If it rains below 0.5 mm / h (millimeters per hour), the sensor does not detect it, and the windscreen wipers should not start. However, if it rains between 0.5 mm / h and 4 mm / h, the windscreen wipers should start at low power. If it rains over 4 mm / h, the windscreen wipers must start at maximum capacity. Finally, since we want to save computing power, we should only read the sensor every two minutes.

Okay, let’s take it step by step and write it down in pseudocode, that is, our job description.

  1. Start rain sensor: The algorithm starts with us starting the sensor we use to measure the amount of rain.
  2. Read sensor: We read the sensor to see how much it rains.
  3. Is it raining? If yes, go to step 4.  If no and sensor does not give a result, go to step 5.
  4. How much does it rain?
    • Is it over 4 mm / h? Start at full power.
    • Is it below 4 mm / h? Start at low power.
  5. Standby mode: Wait 2 minutes, then return to step 2.

Additionally, let’s use a flow chart as well

Exercise Algorithm computer programming

Figure 4: Example of an algorithm illustrated with a flow chart

Summary: Algorithm, flow chart and pseudocode

The algorithm can be seen as a job description of how we should solve a problem and describe the steps the program should take to accomplish that. Either you can write down step by step instructions that the program should execute, so-called pseudocode, or you can draw flow chart when you have more complex problems to solve.  

In short,

  • An algorithm is a job description of how to solve a problem.
  • There are usually several algorithms to solve the same problem.
  • You can visualize an algorithm with a flow chart.
  • Pseudocode is in computer programming a program code-like text used to describe how the algorithm should be formulated.

.The three most common figures in a flowchart are:

Algorithms programming Java

Figure 5: Components in flowcharts

FAQ: Algorithms, flow charts and pseudocode

What exactly is an algorithm?

An algorithm can be seen as a job description of how we should solve a problem. The algorithm describes the order in which things on that job description should be performed. With a more formal description, the definition of an algorithm is a step-by-step procedure of well-defined executable instructions intended to perform a task or solve a problem.

Do I have to use flow charts and draw figures when programming?

No. Flow charts is a tool that should make it easier when you are programming to structure your code and get an overview of how all parts are connected. It is not a must, and simply a technique you can use.

Is there a particular language in pseudocode you should use?

There is no general standard for pseudocode, but the important thing is to be clear and consistent. A good rule of thumb might be to ask yourself, would someone with comparable knowledge that you understand your pseudocode. In other words, keep it simple and straightforward so that someone else can understand your pseudocode.