Using algorithms

It is recommended that you watch the ‘Introduction to algorithms’ video in the previous step before reading this article. You might also like to use a notepad, or a text editor program to write your own algorithms as you read through the article.

An algorithm is an ordered set of steps that solve a given problem. An algorithm could be developed to identify all the steps that you would need to complete to make a cup of tea, or to bake a cake. Likewise, algorithms can be developed to solve computing problems. Algorithms are written in human readable language (that is, not using a programming language).

Let’s revisit an example we looked at last week: a queue at a bus stop.

Can you write down the algorithm to solve the problem faced by the driver of this bus in letting in and out all these passengers?

After writing an algorithm , you need to check that you have covered all possibilities.

What if the bus was already full when it arrived at this bus stop?
This is handled by Line 4 of the algorithm where the code checks whether there is space in the bus to accept any more passengers. In the given situation, because there is no more space, condition in Line 4 fails. This means that no passengers will be accepted on board and the algorithm will reach Line 14 and end.

Can you think of any possibility that is not covered by the algorithm? Can you improve the algorithm to handle that situation too?

Another real life example is playing a game of cards. In this particular game there is one player, and a point is awarded for each pair of cards (two red queens, two black queens etc) that can be found in the first 20 cards selected from a stack of cards.

Line 14: Place the card on the table (if we get here that means we have checked the whole set of cards but could not find a card to pair with this card)

Line 15: If there are cards left in the stack

Line 16: Repeat the steps from Line 5

Line 17: End

Let’s look at another example from last week:
Say we have myArray which is an array that can hold three integer values. You need to assign (array index + 1) into each of the elements.

In the beginning we have:

myArray

0

1

2

Once we have assigned (array index + 1) into each of the elements, the array should look like this:

myArray

0

1

1

2

2

3

An algorithm can be developed to do this.

Algorithm:

Begin

For each element in myArray:

Assign element value as element value = array index + 1

End.

If you want to develop a program to perform this, what you have to do is look at the algorithm and convert that logic into the programming language you are using. Here’s how you can convert this algorithm to a Java program.

/*declares an array of integers and allocates spaces*/int[]myArray;myArray=newint[3];for(intmyIndex=0;myIndex<3;myIndex++){/* assigns a value of array index + 1 to each array
element*/myArray[myIndex]=myIndex+1;}

Fantastic, now you can work out algorithms! Why not think about some of your daily tasks and come up with an algorithm that explains the steps required to complete it.