Standard Algorithms

An algorithm is a series of steps of which when followed will accomplish a task or solve a problem. This could be anything such as a recipe to make a cake or how to fix a tire on a bike. A standard algorithm is something that appears repeatedly in programs. An example of a standard algorithm is Input Validation. Other examples that I will be explaining in this post are linear search, count occurrences and find minimum/maximum.

Linear Search

This searches through a list and it compares the answer with the search criteria and displays the position. If the item is not existent in that particular list then an appropriate message will be displayed, e.g. search item was not found.

Example 1

Linear search for the number 2

Is number 1 search item? No, moves on one place

Is number 2 search item? Yes. Search item is in position 2

Example 2

Linear search for the number 11

Number 11 is not in list

Algorithm:

get search_item

repeat for all items in the list

if current_item = search_number THEN

position = current_item position

exit loop

end if

END REPEAT

DISPLAY position

This video explains Linear Search even further

Count Occurrences

This basically counts how many times an item appears in a list. Every time the search item is found then + 1 is added to the total. With this standard algorithm the total must be set to 0 before beginning. When it finishes the program will display how many times the search item appeared. Count occurrences is useful for lists with many numbers in it as the user doesn’t need to count how many times that number appears, it’s done for them.

Example 1

2, 7, 9, 2, 8, 2, 15, 7, 11, 1

Count occurrences of the number 2

There are three occurrences of the number 2

Example 2

Count occurrences of the number 7

There are two occurrences of the number 7

Algorithm:

Get search_item

Occurrences = 0

REPEAT for all items in the list

IF current_item = search_item THEN

Occurrences = occurrences + 1

END IF

END REPEAT

DISPLAY occurrences

A video of someone giving an explanation of count occurrences

Find Minimum/Maximum

This is used to find the lowest and highest number in a list. When initialising  the total at the start the maximum will always be 0 and the minimum can be 999.

1, 2, 3, 4, 5, 6, 7, 8, 9

Minimum = 1 Maximum = 9

Algorithm for maximum:

maximum = o

REPEAT for all items in the list

IF current_item > maximum THEN

maximum = current_item

END IF

END REPEAT

Display maximum

Algorithm for Minimum:

Minimum = 999

REPEAT for all items in the list

IF current_item < minimum THEN

minimum = current_item

END IF

END REPEAT

Display minimum

As you can see both algorithms are almost identical with minor differences.  

A video description of find min/max

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s