What is an Algorithm?

Algorithms are sets of step-by-step instructions for the computer to follow. They are at the heart of all computer programs.

You can think of an algorithm as similar to a recipe for making something to eat.

If you make a sandwich, you follow a set of steps to put the different ingredients together.

  1. You bring ingredients together
  2. Assemble them as you like
  3. Produce a final product – the sandwich.

If you were asked to write down instructions to make a sandwich, you could create a written algorithm.

Types of Algorithms

Algorithms can be incredibly useful when used in programming. They can be used to sort data into logical orders, some examples of these sorts are Merge, Bubble, and Insertion.

Merge Sort

Bubble Sort

Insertion Sort

Which is the most efficient Algorithm?

In terms of which Algorithm is the most efficient, well it does really depend on its implementation and the amount or complexity of the data it is sorting. The video below shows an example of sorting Algorithm sorting different  types of data.

Trace Tables

When a program you have written is run, it may always not behave as expected.

One way to check and troubleshoot your code is to perform a dry run using a Trace Table.

Trace tables can be used by programmers to track the values of variables as they change throughout a program. This is useful when a program is not producing the desired result.

You create a simple table, showing your program lines, inputs, outputs and variables. You then move through the program line by line changing the values appropriately.

You can see an example of a trace table below running through the program testing the variable values.