Algorithm Design

Most computer programs follow sets of rules so that they can operate perfectly every day without human interference. These rules are called Algorithms. Before any code is written, these rules must be identified and formalised in a process referred to as ‘Algorithm Design’.

In this lesson, we will learn about the process of designing algorithms, which includes the following:

  1. What is an algorithm
  2. Designing an algorithm

Media Attachments: Presentation

1. What is an Algorithm

What is an Algorithm

An algorithm is a description, independent of language, of a process that achieves some task.  The idea is that you could give an algorithm, written in a text file, to any programmer and then he can build your program in whatever language – Python, C#, VB.NET, Pascal, C++, Java, etc.

It can also be thought of as a sequence of steps, independent of any programming language that can be performed over and over again, to achieve a task, almost like a set of rules.

We follow algorithms every day without thinking. Consider an algorithm for crossing the road:

  1. Stand and wait at the traffic lights
  2. Push the pedestrian button
  3. Wait
  4. When the Little Man goes Green
  5. Cross the road

Is this algorithm correct? Could you follow these rules every time you cross the road?

Further Thoughts

The above algorithm is a good start for the rules we follow for crossing the road. However, what if the crossing button has already been pressed? What if there are no cars coming?

How can this algorithm be modified to cope with all possibilities of crossing the road?

2. Designing an Algorithm

Designing an Algorithm

In order to design an algorithm, you need to follow some basic steps.

Let us consider our algorithm from Lesson 2 for a teacher logging into an Information Management System and searching for a student.

The teacher will need to log in using a username and password.

The teacher will be given a list of matching students from which they need to select the correct one. Once they select a student, the student’s ID will be used to display the full details.

Step 1 Understanding the problem

Make sure you fully understand the problem you have been set. A teacher needs to log in, search by surname, pick from a list and view the correct data. If you do not fully understand the problem, you need to go back to your Client.

Step 2 Identify the inputs

You need to identify the type and values of the data. These can correspond to NOUNS in a problem statement e.g. “search by SURNAME” indicates a noun input.

First is a username and password, they will be a value as words or words and numbers.

Then there is the student surname, this will be words, this is shown in figure 3.1.

The Input, output and process for searching the student database for the student with the surname ‘Smith’
Figure 3.1 The Input, output and process for searching the student database for the student with the surname ‘Smith’. Note how the output is a list of students with a matching surname.

Finally, there will be the student ID selected from the list, this is shown in figure 3.2.

Step 3 Identify the Processes

Are there any calculations, or is there any other computational operations going on? Are there any? These usually correspond to verbs in a problem statement e.g. “SEARCH by surname” indicates a verb process.

The username and password will be used to search the database to see if there is a match.

The student name will be used to search the databases to see if there are any matches.

The selected student ID will be used to search the database to retrieve the student details.

Step 4 Identify the Data storage

There has to be a database, containing a list of usernames and passwords, and a list of students with all of their contact details as well as their student IDs.

We don’t know if this is going to be one or two databases at this time.

The second search using ID to extract data about a single student from the database and retrieve full student details.
Figure 3.2 The second search using ID to extract data about a single student from the database and retrieve full student details.

Step 5 Identify the Outputs

The first output will be either a confirmation or rejection of the teacher login.

The second output will be a list of students with a matching surname to the input. This is shown in figure 3.1

The final output will be the contact details of the student selected from the list, shown in figure 3.2

Step 6 Collect your notes

Organise your data into a Quad Diagram, this is simply four boxes showing the Inputs, Processes, Outputs and Data storage required by your algorithm.

A Quad Diagram showing all the inputs, outputs process and data storage for the student search algorithm.
Figure 3.3 A Quad Diagram showing all the inputs, outputs process and data storage for the student search algorithm.

Further Thought

Produce a Quad Diagram for the Student Planner Program identified in Lesson 1, showing Input, Output, Process and Data Storage.

Lesson Summary

So to summarise what we have learned in this lesson:

  • Algorithms are sets of rules to follow to solve a particular problem.
  • They are an operational set of steps that can be followed repeatedly.
  • Algorithm design can be broken down into 5 distinct steps:
    • Understanding the problem – do you know exactly what you are being asked to do
    • Identify the inputs – what data needs to go into your program
    • Identify the processes – are there any calculations or computational operations happening?
    • Identify the data storage – do you need a database or a text file?
    • Identify the outputs – what data is coming out of your program?
    • Collect your notes – put together a quad diagram to gather your thoughts.