5.3

# 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?

‘bus queue’ by brapps from https://www.flickr.com/photos/brapps/233148352/in/photostream/ used under the CC BY-NC 2.0 licence

An algorithm to solve this problem would look something like this:

• Line 1: Begin
• Line 2: Check whether there are passengers who are getting off
• Line 3: If so let them off the bus
• Line 4: If the bus has space for more passengers
• Line 5: For each passenger getting on the bus
• Line 6: Check whether they have a valid ticket/pass
• Line 7: If they have a valid ticket/pass
• Line 8: Let them on the bus
• Line 9: Otherwise issue a ticket/pass and let them in the bus
• Line 10: Check whether the bus has space for more passengers
• Line 11: If there is no space stop boarding passengers
• Line 12: Otherwise continue boarding passengers
• Line 13: Otherwise (bus has no space) Stop boarding passengers
• Line 14: End

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.

Can you write the algorithm to play this game?

‘Cards’ by taestell from https://www.flickr.com/photos/taestell/8215427037/in/photostream/used under the CC BY-NC 2.0 licence

See if your algorithm matches the one below:

• Line 1: Begin
• Line 2: Shuffle the set of cards
• Line 3: Set score to zero
• Line 4: Select the first 20 cards from the stack and discard the rest
• Line 5: Take a card
• Line 6: For each remaining card in the stack
• Line 7: Compare the card with the selected card
• Line 8: If they make a pair
• Line 9: Increase the score by one
• Line 10: Place the paired cards on the table
• Line 11: If there are more cards in the stack
• Line 12: Take the first card from the stack to replace the previous
• Line 13: Otherwise (there are no more cards) stop
• 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 = new int[3];

for (int myIndex = 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.