Archive for the ‘Standard Algorithms’ Category

Standard Algorithms 3 & 4

December 3, 2007

So these two find the highest number in a list or the lowest number in a list.  The way they work is this:  look at the first item in the list and make it the maximum, then look at every other item and see if it is more.  Quite simple when you think of it like that.

Here is the pseudocode for finding max:

1.1  set the first item to be the maximum

 

1.2  for each item (starting with the second)

 

1.3  if the current item is more than the maximum

 

1.4  set the current item to be the maximum

 

1.5  end if

 

1.6  end loop

 

1.7 print out the maximum

 

 Finding the minimum is exactly the same only change more than to read less than (and every maximum to minimum).

 

Powerpoint:

 finding-max-min.jpg

 

 

 

 

Standard Algorithm 2 – Linear Search

November 30, 2007

This algorithm asks the user for something to look for and then checks through a list of items to see if it can find it. If it does find it, the algorithm says in what position it found it. So for example from the following list:

“Stewart”, “Simon”, “Dara”, “Scott”, “Keith”, “Farooq”, “Gaetano”

User Input Algorithm Output

Stewart 1

Dara 3

Gaetano 7

In the same way as the last standard algorithm you need to be able to write the pseudocode in the exam!

1.1 ask for item being searched for

 

1.2 for each item in the array

 

1.3 if this item matches the item being searched for

1.4 store the position

 

1.5 end if

 

1.6 end loop

 

1.7 print out where the item was found

 

Powerpoint below:

linear-search.jpg

Standard Algorithm 1 – Counting Occurrences

November 29, 2007

This is the first standard algorithm (in no particular order) that you have to know, and be able to write a pseudo code version of in the exam.

Put simply this algorithm counts the number of times an item appears in a list. The item could be a name or a number, the list is normally an array.

So for example in the following list:

“A” “B” “B” “C” “B” “A” “A” “B” “C” “B”

The algorithm would ask the user what they are looking for and then should return the results below:

Being looked for                             Number of occurrences

A                                                                          3

B                                                                          5

C                                                                          2

So you need to be able to write the pseudo code that will do this. My answer is below, see the powerpoints at the bottom for more detail:

1.1 ask for item being searched for

1.2 for each item in the array

1.3 if this item matches the item being searched for

1.4 add one to the count

1.5 end if

1.6 end loop

1.7 print out the number of times the item was found

 

counting-occurrences.jpg